Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create ENUM in 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

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

AttributeError: 'tuple' object has no attribute '_set_parent_with_dispatch'
like image 275
TIMEX Avatar asked Dec 17 '13 20:12

TIMEX


1 Answers

You need pass race_enum as an argument for Enum, not Column

You can either pass in your tuple as whole

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

or use * to unpack race_enums:

Column('race', Enum(*race_enums))
like image 59
Wondercricket Avatar answered Oct 16 '22 06:10

Wondercricket