Type "..." already exists during table creation
andrei-shabanski opened this issue · 5 comments
I tried the example code from README and got an error:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateObject) type "myenum" already exists
[SQL: CREATE TYPE myenum AS ENUM ('one', 'two', 'three')]
(Background on this error at: https://sqlalche.me/e/14/f405)
Any suggestion on what could go wrong?
import enum
import sqlalchemy as sa
class MyEnum(enum.Enum):
one = 1
two = 2
three = 3
class ExampleTable(Model):
__tablename__ = 'example_table'
test_field = sa.Column(sa.Integer, primary_key=True, autoincrement=False)
enum_field = sa.Column(sa.Enum(MyEnum))
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
sa.Enum('one', 'two', 'three', name='myenum').create(op.get_bind())
op.create_table('example_table',
sa.Column('test_field', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('enum_field', sa.Enum('one', 'two', 'three', name='myenum'), nullable=True),
sa.PrimaryKeyConstraint('test_field')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('example_table')
sa.Enum('one', 'two', 'three', name='myenum').drop(op.get_bind())
# ### end Alembic commands ###
alembic 1.11.3
alembic-postgresql-enum 0.1.5
SQLAlchemy 1.4.46
Thanks for reporting the issue. I double-checked and you're right - it's not working. I don't know how we didn't notice it, we will correct the documentation soon. Instead of sa.Enum you should use sa.dialects.postgresql.ENUM with create_type=False
Same issue here. Using sa.dialects.postgresql.ENUM
with create_type=False
on the model declarations will keep trying to create the types and fail. Had to manually delete the first lines from the migration file and it works.
Same issue here. Using
sa.dialects.postgresql.ENUM
withcreate_type=False
on the model declarations will keep trying to create the types and fail. Had to manually delete the first lines from the migration file and it works.
It seems that alembic ignores create_type flag when generating migration. Fast solution for you is to add create_type=False
manually into migration.
I am now working on fixing this behaviour
Changes will be available in next 0.1.6 release
0.1.6 is released. Library will work even if sa.Enum is used