如何在SQLAlchemy中创建ENUM?

时间:2013-12-17 20:44:47

标签: python mysql database enums sqlalchemy

from sqlalchemy import *
from migrate import *

meta = MetaData()
race_enums = ('asian','mideastern','black','nativeamerican','indian','pacific','hispanic','white','other');
profiles_profiles = Table(
    'profiles_profiles', meta,
    Column('id', Integer, primary_key = True),
    Column('user_id', Integer, nullable=False, unique=True),
    Column('race', Enum, race_enums),
    Column('summary', Text, nullable= True),
    Column('my_life', Text, nullable= True),
    Column('to_do', Text, nullable= True),
    Column('favs', Text, nullable= True),
    Column('created_at', DateTime, nullable=True),
    Column('updated_at', DateTime, nullable=True)
)

def upgrade(migrate_engine):
    meta.bind = migrate_engine
    profiles_profiles.create()
    pass

def downgrade(migrate_engine):
    meta.bind = migrate_engine
    profiles_profiles.drop()
    pass

当我manage.py升级时,我收到此错误:

AttributeError: 'tuple' object has no attribute '_set_parent_with_dispatch'

1 个答案:

答案 0 :(得分:22)

您需要传递race_enum作为Enum的参数,而不是Column

您可以传递tuple整体

Column('race', Enum('asian','mideastern','black','nativeamerican','indian','pacific','hispanic','white','other'))

或使用*解包race_enums

Column('race', Enum(*race_enums))