Created
January 19, 2011 17:31
-
-
Save mrowl/786501 to your computer and use it in GitHub Desktop.
This defines a new enum type using the sqlalchemy type decorator. Data is stored with the sqlalchemy SmallInteger type while strings are used with the model. Preferable to the varchar (too large) or native enum (pain on postgres).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlalchemy as sa | |
class EnumIntType(sa.types.TypeDecorator): | |
impl = sa.types.SmallInteger | |
values = None | |
def __init__(self, values=None): | |
sa.types.TypeDecorator.__init__(self) | |
self.values = values | |
def process_bind_param(self, value, dialect): | |
return None if value == None else self.values.index(value) | |
def process_result_value(self, value, dialect): | |
return None if value == None else self.values[value] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use is similar to the sqlalchemy Enum type: