Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

SQLAlchemy type to store standard enum.Enum value

Project description

SQLAlchemy-Enum34

https://badge.fury.io/py/SQLAlchemy-Enum34.svg? https://travis-ci.org/spoqa/sqlalchemy-enum34.svg?branch=master https://codecov.io/github/spoqa/sqlalchemy-enum34/coverage.svg?branch=master

This package provides a SQLAlchemy type to store values of standard enum.Enum (which became a part of standard library since Python 3.4). Its internal representation is equivalent to SQLAlchemy’s built-in sqlalchemy.types.Enum, but its Python representation is not a str but enum.Enum.

Note that this works on Python 2.6 as well as 3.4, the latest version of Python, through enum34 package.

The following example shows how enum-typed columns can be declared:

import enum

from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_enum34 import EnumType

Base = declarative_base()

class Color(enum.Enum):
    black = 'black'
    white = 'white'
    navy = 'navy'
    red = 'red'

class Size(enum.Enum):
    small = 'S'
    medium = 'M'
    large = 'L'
    xlarge = 'XL'

class Shirt(Base):
    id = Column(Integer, primary_key=True)
    color = Column(EnumType(Color), nullable=False)
    size = Column(EnumType(Size, name='shirt_size'), nullable=False)

And the following REPL session shows how these columns work:

>>> shirt = session.query(Shirt).filter(Shirt.color == Color.navy).first()
>>> shirt.color
<Color.navy: 'navy'>
>>> shirt.size
<Size.large: 'large'>

Written by Hong Minhee at Spoqa, and distributed under MIT license.

Changelog

2.0.0

Released on April 14, 2021.

  • Update for SQLAlchemy 1.3+ and modern Python versions. [#6 by Joongi Kim]

1.0.1

Released on August 7, 2015.

  • Fixed that sqlalchemy_enum34.Enum didn’t allow nullability. [#1 by Andrés Moya]

1.0.0

First version. Released on July 30, 2015.

Download files

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

Files for SQLAlchemy-Enum34, version 2.0.0
Filename, size File type Python version Upload date Hashes
Filename, size SQLAlchemy_Enum34-2.0.0-py2.py3-none-any.whl (4.3 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size SQLAlchemy-Enum34-2.0.0.tar.gz (3.7 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page