Last Updated: February 25, 2016
·
10.76K
· iaincampbell

Existing PostgreSQL ENUMs in Alembic

If you drop/modify a table or try to add a column using an existing ENUM, alembic will complain:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) type "zone" already exists
"CREATE TYPE zone AS ENUM ('uk_mainland', 'uk_channel_islands' )"

The quick fix is to edit the migration file and add

create_type=False

to the end of the constructor:

sa.Column('zone', postgresql.ENUM('uk_mainland', 'uk_channel_islands', name='zone', create_type=False), nullable=False)

That disables the creation attempt and saves you having to drop the types manually.