I'm having trouble using enums in conjunction with schema_translate_map for postgres migrations.
My model, single table, single enum.
import enum
from sqlalchemy import MetaData, Enum, Column, Integer
from sqlalchemy.ext.declarative import declarative_base
metadata = MetaData()
Base = declarative_base(metadata=metadata)
class Enum1(enum.Enum):
One = 1
Two = 2
class Table1(Base):
__tablename__ = 'table1'
id = Column(Integer, primary_key=True)
type1 = Column(Enum(Enum1))
Version file.
"""initial revision
Revision ID: 844dd0269c1b
Revises:
Create Date: 2020-02-17 12:23:31.125308
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '844dd0269c1b'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('table1',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('type1', sa.Enum('One', 'Two', name='enum1'), nullable=True),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('table1')
# ### end Alembic commands ###
Run migration funtion from env.py (creating foo schema):
def run_migrations_online():
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
# use different schema
connection.execute(f'create schema if not exists foo')
connection = connection.execution_options(
schema_translate_map={None: 'foo'}
)
context.configure(
connection=connection,
target_metadata=target_metadata,
include_schema=True,
)
with context.begin_transaction():
context.run_migrations()
I get the following error when i run alembic upgrade head:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedObject) type "enum1" does not exist
LINE 4: type1 enum1,
^
[SQL:
CREATE TABLE foo.table1 (
id SERIAL NOT NULL,
type1 enum1,
PRIMARY KEY (id)
)
]
(Background on this error at: http://sqlalche.me/e/f405)
It works when I comment out schema_translate_map.
I feel like I'm missing something fundamental about using schema_translate_map for multi-tenant/schema.