Discussion:
Solution!!! ENUM type that works with sqlite too
Ian Charnas
2007-04-22 13:33:35 UTC
Permalink
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 ***@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
-~----------~----~----~----~------~----~------~--~---
Brett
2007-04-23 14:10:21 UTC
Permalink
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/Enum
Post by Ian Charnas
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)
"""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
"""
self.names = names
super(EnumType, self).__init__(*args, **kw)
raise EnumError("Value(%s) not in Enum(%s)" %
(value, ", ".join(self.names)))
return super(EnumType, self).convert_bind_param(value, engine)
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 ***@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
-~----------~----~----~----~------~----~------~--~---

Loading...