python-3.x 在SQLAlchemy中,我可以拥有一个包含多个字符串的列吗?

bhmjp9jg  于 2022-12-14  发布在  Python
关注(0)|答案(1)|浏览(128)

对于下面的代码,我有一个Map和一个“cookie”的例子,它包含了我想通过python添加到SQLAchalemy的一些信息。正如你所看到的,一些字段,如Farmloc、TreasureMap和Crafts都有多个条目。我搜索了一下,除了Enum之外没有找到其他的条目。但是这给我带来了一些麻烦。有没有比我在这里所做的更好的方法呢?使用Python3.6.1和SQLAchalemy 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()
kxe2p93d

kxe2p93d1#

查看内置类sqlalchemy.types.ARRAY
例如:

TreasureMap = Column(ARRAY(String))

旁注:按照惯例,所有列名均为小写,并以分数分隔(例如treasure_mapTreasureMap之上)。

相关问题