I have lots of integers in my existing database which signify an enum. In my python project I have created the corresponding enums like so:
class Status(Enum):
OK = 0
PENDING = 1
CANCELED = 2
DUPLICATE = 3
INCOMING = 4
BLOCKED = 5
In my SQLalchemy models I'd like to be able to do the following
status = Column(Status, nullable=False)
Which should convert the database integers to python enums and back transparantly
So far I've tried to inherit from the sqlalchemy.types.TypeDecorator and overwrite the process_bind_param and process_result_value to cast the int back and forwards.
This would however mean I would have to repeat this for every Enum in violation of the DRY principle.
I would thus prefer a Mixin class which inherits sqlalchemy.types.TypeDecorator and overwrites the two methods to cast to and from the Enum which inherits this mixin.
Any ideas on how to approach this?