【问题标题】:In SQLalchemy, can I have a column with multiple strings?在 SQLalchemy 中,我可以有一个包含多个字符串的列吗?
【发布时间】:2018-01-18 03:48:17
【问题描述】:

对于以下代码,我有一个映射和一个“cookie”示例,其中包含我想通过 python 添加到 SQLalchemy 的一些信息。如您所见,Farmloc、TreasureMap 和 Crafts 等字段都有它们指向的多个条目。我搜索并找不到除 Enum 之外的其他内容,但这给我带来了一些麻烦。有没有比我在这里所做的更好的方法。使用 Python3.6.1 和 SQLalchemy 1.1.11

import csv, sqlalchemy
from sqlalchemy import Column, String, Integer, ForeignKey, Numeric, Boolean
from sqlalchemy.ext.declarative import declarative_base
import sqlite3
from enum import Enum, auto
import enum
from sqlalchemy import create_engine

    engine = create_engine('sqlite:///:memory:', echo=True) #TEST DB

    Base = declarative_base()

    class Crystal(Base):
    __tablename__ = 'crystals'
    dbID = Column(Integer, primary_key=True)
    ItemName = Column(String, index=True, nullable=False) #Full name, (ie. Earth Shard, Wind Crystal, Fire Cluster)
    ItemType = Column(String) #Fire, Earth, Wind, Ice, Water ONLY
    ItemPow = Column(String) #Shard, Crystal, Cluster ONLY
    Farmloc = Column(String) #Where player can farm
    Retainer = Column(Boolean) #If retainer can farm
    RetainerQ = Column(Integer) #Quantity retainer gets
    Levee = Column(Boolean) #Any Levees rewards these.
    TreasureMap = Column(String) #False=NO, otherwise Types listed
    Desynthed = Column(String) #False=NO, otherwise Herb1, Herb2, Ore1, etc
    Crafts = Column(String) #Crafts associated with (ie Earth w LWR)
    Price = Column(Integer) #MB price (Should be used as ref for all craftables)

from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()

cc_cookie = Crystal(dbID = 1,
                        ItemName= 'Wind Cluster',
                        ItemType = 'Wind',
                        ItemPow = 'Cluster',
                        Farmloc = 'The Dravanian Hinterlands, Mor Dhona',
                        Retainer = False,
                        RetainerQ = 0,
                        Levee = False,
                        TreasureMap = 'Dragonskin Treasure Map,Gaganaskin Treasure Map,Gazelleskin Treasure Map,Leather Buried Treasure Map',
                        Desynthed = 'Clary Sage,Furymint,Highland Oregano,Windtea Leaves',
                        Crafts = 'CRP,GSM,LWR,WVR',
                        Price = 500)       


Base.metadata.create_all(engine)

session.add(cc_cookie)
session.commit()

【问题讨论】:

  • 您打算继续使用 sqlite,还是长期使用其他数据库?
  • 我对数据库很陌生。现在我会坚持下去,除非你有别的想法。
  • 你可以在 sqlite 中使用数组列,尽管它没有原生支持。你可以store JSON in a text columnPostgreSQL 有真正的数组类型,如果你有一天能做到这一点。
  • 好的。那我会考虑我的选择。谢谢!

标签: python python-3.x sqlalchemy


【解决方案1】:

查看内置的class sqlalchemy.types.ARRAY

例如:

TreasureMap = Column(ARRAY(String))

旁注:按照惯例,所有列名都是小写的,并且在分数下分隔(例如 treasure_map 超过 TreasureMap。)

【讨论】:

  • 谢谢。我在文档中遇到了那一点,但这是让我什至没有跳进去的那一行。 “这种类型是所有 ARRAY 操作的基础。但是,目前只有 PostgreSQL 后端支持 SQLAlchemy 中的 SQL 数组” PostgreSQL 只是一个导入还是像 SQLalchemy 一样的 SQL 系统?
  • PostgreSQL 是一个数据库,SQLAlchemy 是一个 SQL 客户端库/ORM。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 2011-09-24
  • 2012-01-05
  • 1970-01-01
  • 2019-08-14
相关资源
最近更新 更多