Making Python enums into SQLAlchemy tables with support for Alembic migrations
Project description
SQLAlchemy has built-in enum.Enum support, via its column type sqlalchemy.Enum. However, this type relies either on the backend’s enum type, or on a check constraints. Both of these are immutable objects, which are a pain in the butt to modify (only PostgreSQL supports adding values to an enum type, and even then it doesn’t support removing them).
Another often-used pattern to support enums in a database is via a dedicated table that reflects the enum values. This requires updating the table everytime the enum is modified, but doing so is much simpler than replacing a type.
This package allows you to create the enum table, and columns referencing that table, directly from a Python enum class. It also interfaces with Alembic to automatically add INSERT and DELETE statements to your autogenerated migration scripts.
When to use
Only works with Pythons’s enumeration classes, or at least one with a behavior similar to enum.Enum. Does not work with collections of arbitrary entries.
Only works with SqlAlchemy’s declarative ORM system. If you only use SqlAlchemy Core… deal with it.
Better used for frequently updated enumeration classes.
Do not use with another package that provides op.enum_insert and op.enum_delete operations in Alembic.
How to use with SqlAlchemy
import enum import sqlalchemy as sa from sqlalchemy.ext.declarative import declarative_base import enumtables as et # Create the Python enumeration class class MyEnum(enum.Enum): HELLO = "HELLO" WORLD = "WORLD" Base = declarative_base() # Create the enumeration table # Pass your enum class and the SQLAlchemy declarative base to enumtables.EnumTable MyEnumTable = et.EnumTable(MyEnum, Base) # Create a model class that uses the enum class MyModel(Base): __tablename__ = "my_model" # Pass the enum table (not the enum class) to enumtables.EnumColumn # It replaces sqlalchemy.Column, but aside from the enum table, # it can take the same parameters. # It will automatically create a ForeignKeyConstraint referencing the enum table. enum_value = et.EnumColumn(MyEnumTable, primary_key = True) # When valued (on an instance of MyModel), enum_value will be an instance of MyEnum.
First, the EnumTable factory takes the enum class and the declarative base class to create the actual ORM class. Then this ORM class is passed to the EnumColumn class to create the column linked to the enum table. The column behaves just as if it had SqlAlchemy’s own Enum type.
On the implementation side, EnumTable is not a class, it’s a factory function that performs Python black magic to create a subclass of the declarative base, and set it up to be a DB table containing the enum items (actually it just has one column item_id of type String).
EnumColumn is a subclass of SqlAlchemy’s Column that gets initialized with a custom type and a foreign key to the enum table.
How to use with Alembic
First add:
import enumtables
at the begining of your env.py file, then add the same line in the imports of your script.py.mako file. The package uses Alembic’s standard hooks to take care of migration generation.
Don’t forget to review the migrations afterwards. Especially make sure that, if the table did not exist before, the op.enum_insert commands are located after the corresponding op.create_table command.
Other uses
Using the enum table class directly
The enum table class behaves like any SqlAlchemy ORM class:
enum_query = session.query(MyEnumTable) result = enum_query.first() # The column item_id stores the name of the enum item as a string enum_name = result.item_id
Adding more columns to the enum tables
Any keyword argument passed to the EnumTable factory becomes a member of the table class. Which means, you can pass anything (like a column) exactly as you would defined a usual ORM class:
BetterEnumTable = et.EnumTable( MyEnum, Base, # tablename is turned into __tablename__ tablename = "better_enum", # Let's add a new column! order = sa.Column(sa.Integer, nullable = False), # And since it's an ordering number, let's make it unique too. __table_args__ = ( sa.UniqueConstraint('order'), ), )
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for SqlAlchemy Enum Tables-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e732b43292272a310038eb47e663437aa161ea7635f50ae3e6f745f24ef9012 |
|
MD5 | a0ced16a617fa58b05c7ee688e025bff |
|
BLAKE2b-256 | 1d742b1e17626aa0977e4bfbb8874029a8b20051f65579c87bef9b614fd0e141 |
Hashes for SqlAlchemy_Enum_Tables-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 534ea41df17c8d0cedde3118b24f07b032ecf63ac43fa04610aa92cbf1e7470d |
|
MD5 | ff8930b0af7a3a056a318b61f31ae10e |
|
BLAKE2b-256 | 7aae52c8f34dbe81e269eea451d80d2a43f5dadc08676c535b3b361e2d1fc4fd |