sqlalchemy icon indicating copy to clipboard operation
sqlalchemy copied to clipboard

add "create_type" to sqltypes.Enum ?

Open zzzeek opened this issue 3 years ago • 14 comments

this type has a "name" argument that only applies to PostgreSQL. So the type already has "backend specific" arguments, and it having a name implies directly that there's a separate CREATE statement. so we should have create_type directly present. See https://github.com/sqlalchemy/alembic/issues/1347

zzzeek avatar Nov 08 '23 17:11 zzzeek

it probably makes sense. don't we support actual enum also in another db? Mariadb/Mysql supports them https://mariadb.com/kb/en/enum/ too, but I'm not sure if they need to be created or not

CaselIT avatar Nov 08 '23 19:11 CaselIT

Hello all,

I would like to take this issue.

I can investigate this and try to solve.

francipvb avatar Aug 25 '24 23:08 francipvb

Feel free to ask question here or on gitter. Emulated types like enum are not the easiest to work with, but this issue may be easy enough!

CaselIT avatar Aug 26 '24 07:08 CaselIT

This will be quite helpful!

cobnett3 avatar Oct 19 '24 10:10 cobnett3

Mike Bayer has proposed a fix for this issue in the main branch:

add create_type to Enum https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/6162

sqla-tester avatar Sep 21 '25 16:09 sqla-tester

Sorry to revive this, but I guess I'm missing something. Is that proposed fix is already up?

When I try to use sqlalchemy.Enum("OK", "NICE", name="my_enum", create_type=False) I still get the same error mentioned in https://github.com/sqlalchemy/alembic/issues/1347 if my enum is already created.

Using postgresql.ENUM works fine...

NathanAP avatar Nov 28 '25 14:11 NathanAP

are you using the latest 2.1 branch from main? that's where this is

zzzeek avatar Nov 28 '25 17:11 zzzeek

AH! Ok that makes sense, thanks 💪🏻

NathanAP avatar Nov 28 '25 18:11 NathanAP

Hey, I am using the main branch and it seems to be handling ENUMs correctly with create_type = False. The problem is that it adds metadata=MetaData() to the migration file but it does not auto import from sqlalchemy import MetaData.

DRKolev-code avatar Jan 20 '26 20:01 DRKolev-code

~~if you are indicating metadata=MetaData() in your Enum (which is unnecessary?) add that import to your alembic template.~~

~~but....there shouldn't be any need to set an empty MetaData on your Enum~~ this happens due to the Table

>>> from sqlalchemy import Enum, MetaData
>>> repr(Enum("x", "y", "z", create_type=False))
"Enum('x', 'y', 'z', create_type=False)"
>>> repr(Enum("x", "y", "z", create_type=False, metadata=MetaData()))
"Enum('x', 'y', 'z', metadata=MetaData(), create_type=False)"

zzzeek avatar Jan 20 '26 21:01 zzzeek

Hey, I am using the main branch and it seems to be handling ENUMs correctly with create_type = False. The problem is that it adds metadata=MetaData() to the migration file but it does not auto import from sqlalchemy import MetaData.

@CaselIT I think this is a 2.1 regression ^^^^

>>> from sqlalchemy import Table, Column, Enum, MetaData
>>> e1 = Enum("x", "y", "z", create_type=False)
>>> t = Table("t", MetaData(), Column("q", e1))
>>> repr(e1)
"Enum('x', 'y', 'z', metadata=MetaData(), create_type=False)"

it doesnt make sense for it to repr() like that (for one, it's wrong, that's not a blank MetaData, it's the one that's on the Table)

zzzeek avatar Jan 20 '26 21:01 zzzeek

I agree, we can likely remove metadata from the repr

CaselIT avatar Jan 20 '26 21:01 CaselIT

Federico Caselli referenced this issue:

Omit metadata kw from Enum repr https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/6568

sqla-tester avatar Jan 22 '26 20:01 sqla-tester

note that alembic PR https://github.com/sqlalchemy/alembic/pull/1795 for alembic issue https://github.com/sqlalchemy/sqlalchemy/issues/13140 likely fixes this

zzzeek avatar Feb 22 '26 20:02 zzzeek