Add Integer Enum type support

Issue #3713 wontfix
Михаил Доронин created an issue

There are attempts to do this already.

https://gist.github.com/shazow/594861

http://sqlalchemy-utils.readthedocs.io/en/latest/data_types.html#module-sqlalchemy_utils.types.choice

But I think that this should be part of sqlalchemy really.

Probably everybody knows this but I will state the benefits once more:

  1. Takes less space in database
  2. Provides higher level interface for cases where for the reason #1 INTEGER, TINYINT etc have been used instead of ENUM or VARCHAR where conceptually we are dealling with enum.

Can't really think of everything else right now, but that's should be enough.

I can try to add this functionality. I don't know sqlalchemy internals well enough, but the gist above should help and it's shouldn't be that hard in any case I think.

The main reason I've maid issue instead of creating pull request right away is that I have a questions about implementation.

  1. Should we extend existing Enum type or create a new one? Both ways would work of course, but in the former case would complicate Enum interface a bit (maybe)

So I see it this way

if native_enum param is False then we look if there is additional 'type' param. 'type' can be one of sqlalchemy types suiting Enum or even database specific type like TINYINT or it could be 'auto', in which case sqlalchemy would choose type based on the amount of enum members (like if we have 3 members that it can use something like TINYINT(2)).

Default value for 'type' param would be varchar so old code wouldn't be broken.

Another option is to create a new IntEnum type with similar interface.

Comments (4)

  1. Mike Bayer repo owner
    • changed component to sql

    -1 because there is no enumerated integer type that is provided by databases. This is an extension type that is easily implemented using TypeDecorator and the SQLAlchemy project should not be in the business of supporting convenience types like this. sqlalchemy-utils and similar are great places for this kind of thing.

  2. Mike Bayer repo owner

    also a normal ENUM that uses a single-character value takes up less space than an integer.

  3. Михаил Доронин reporter

    @zzzeek Okay, then utils it is. Obviously single character enum is takes less space than integer, but not less than TINYINT(3) which would give 8 members. So storage wise it's a good thing to have.

  4. Log in to comment