alembic: enum already exists

Describe the bug

Whenever I am adding Enum field in a new model it’s giving me this error “sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateObject) type “tickettype” already exists” for resolving this I have to delete DB and create a new one.

Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected

https://docs.sqlalchemy.org/en/14/errors.html#error-f405

SQLAlchemy Version in Use

1.4.46

DBAPI (i.e. the database driver)

2.9.5

Database Vendor and Major Version

2.9.5

Python Version

3.8

Operating system

Linux

To Reproduce

alembic revision --autogenerate -m "New Migration"
alemic upgrade head

Error

# Copy the complete stack trace and error message here, including SQL log output if applicable.

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateObject) type “tickettype” already exists

[SQL: CREATE TYPE tickettype AS ENUM (‘PAID’, ‘FREE’, ‘GROUP’, ‘DONATION’, ‘REQUEST’, ‘GUESTLIST’)]

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 2
  • Comments: 17 (7 by maintainers)

Most upvoted comments

you are using the enum right here

sa.Column(
            "status", project_status_enum, nullable=False, server_default="active"
        ),

that’s why it gets created. please see also https://github.com/sqlalchemy/alembic/issues/1347 if you want to use create_type and similar PG only options

alembic doesnt generate migrations to add enums. The enum gets created only if you run CREATE TABLE via op.create_table(). I dont even see any code that would do this for add_column().

this isn’t a bug as we dont support adding elements to PG enums, you would have to do this manually using op.execute("ALTER ENUM myenum ADD newitem")

Where i have to add this?

In the revision file that is generated by Alembic, where you have upgrade() and downgrade() defined. You write it inside upgrade(). op should be defined in the file already.