如何在SQLAlchemy中创建ENUM? [英] How to create ENUM in SQLAlchemy?

查看:2189
本文介绍了如何在SQLAlchemy中创建ENUM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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升级这个,我得到这个错误:

When I manage.py upgrade this, I get this error:

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


推荐答案

您需要传递 race_enum 作为枚举

您可以传递元组整体

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


$ b b

或使用 * 解开 race_enums

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

这篇关于如何在SQLAlchemy中创建ENUM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆