Getting ENUM-like behavior from a MySQL VARCHAR

37 views
Skip to first unread message

Ryan

unread,
Nov 9, 2011, 8:27:07 PM11/9/11
to sqlal...@googlegroups.com
I have a MySQL VARCHAR column, but I'd like to get ENUM-like behavior at the ORM level. I'm using the declarative style. Here's what I've got so far:

language = Column(Enum('en', 'fr', native_enum=False), CheckConstraint(), default='en')

Docs say that when native_enum is set to False, uses VARCHAR + check constraint for all backends.

What args do I need to pass to CheckConstraint in order to restrict the list of languages?

Michael Bayer

unread,
Nov 10, 2011, 6:39:47 PM11/10/11
to sqlal...@googlegroups.com
the CHECK constraint is generated automatically by the Enum type:

class A(Base):
    __tablename__= 'a'
    id = Column(Integer, primary_key=True)
    language = Column(Enum('en', 'fr', native_enum=False), default='en')
e = create_engine("mysql://scott:tiger@localhost/test", echo=True)
Base.metadata.create_all(e)

output:

CREATE TABLE a (
id INTEGER NOT NULL AUTO_INCREMENT, 
language VARCHAR(2), 
PRIMARY KEY (id), 
CHECK (language IN ('en', 'fr'))
)








--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/9upUrF4h5-QJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Reply all
Reply to author
Forward
0 new messages