Skip to main content

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

  1. 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.

  2. Only works with SqlAlchemy’s declarative ORM system. If you only use SqlAlchemy Core… deal with it.

  3. Better used for frequently updated enumeration classes.

  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SqlAlchemy Enum Tables-1.0.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

SqlAlchemy_Enum_Tables-1.0.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file SqlAlchemy Enum Tables-1.0.0.tar.gz.

File metadata

  • Download URL: SqlAlchemy Enum Tables-1.0.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for SqlAlchemy Enum Tables-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4e732b43292272a310038eb47e663437aa161ea7635f50ae3e6f745f24ef9012
MD5 a0ced16a617fa58b05c7ee688e025bff
BLAKE2b-256 1d742b1e17626aa0977e4bfbb8874029a8b20051f65579c87bef9b614fd0e141

See more details on using hashes here.

File details

Details for the file SqlAlchemy_Enum_Tables-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: SqlAlchemy_Enum_Tables-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for SqlAlchemy_Enum_Tables-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 534ea41df17c8d0cedde3118b24f07b032ecf63ac43fa04610aa92cbf1e7470d
MD5 ff8930b0af7a3a056a318b61f31ae10e
BLAKE2b-256 7aae52c8f34dbe81e269eea451d80d2a43f5dadc08676c535b3b361e2d1fc4fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page