http://www.sqlalchemy.org/trac/wiki/UsageRecipes/Enum
On Sun, 2007-04-22 at 13:33 +0000, Ian Charnas wrote: > I'm sure a lot of us have done something like this, I figured I'd post > it so people can find it in a google search and won't have to write it > and debug it themselves... This is a Type that emulates ENUM for > engines (like sqlite) that don't have a native ENUM type. This > *could* be augmented so that it uses the actual ENUM type for engines > that support it. For those interested, you'd add a "get_col_spec" > method (see other types in sqlalchemy/types.py for details) > > class EnumType(Unicode): > """Basic type that emulates ENUM sql type. > > Useful for database engines that don't support ENUM (such as > sqlite). > required 'names' parameter must be a list of strings for this ENUM > > """ > def __init__(self, names, *args, **kw): > self.names = names > super(EnumType, self).__init__(*args, **kw) > def convert_bind_param(self, value, engine): > if value is not None and value not in self.names: > raise EnumError("Value(%s) not in Enum(%s)" % > (value, ", ".join(self.names))) > return super(EnumType, self).convert_bind_param(value, engine) > > and this is how you'd use it: > > customers_table = Table('customer', metadata, > Column('name', String(50)), > Column('frequency', EnumType(names=['Rare', 'Occasional', > 'Regular']), default='Rare'), > ) > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---