enum type that works with sqlite too

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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'),
)