在SQLalchemy中,我可以有一个包含多个字符串的列吗?

时间:2017-08-10 04:28:12

标签: python python-3.x sqlalchemy

对于以下代码,我有一个" 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()

1 个答案:

答案 0 :(得分:3)

查看内置数组类型 - http://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.ARRAY

例如:

TreasureMap = Column(Array(String))

附注:根据惯例,所有列名都是小写且分数低于分数(例如treasure_map超过TreasureMap。)