I have a migration that works on a generic schema named tenant_schema
.
In the run_migrations_online
function in env.py
i set up a schema_translate_map for tenant_schema
.
I expected sqlalchemy
to translate this migration operation to run on the desired schema, however it seems like it tries to run sql queries using the schema tenant_schema
.
any ideas how to fix it ?
example:
the upgrade function in the migration file:
2018-09-05_17-28_247f3546088f_add_foo_column.py
def upgrade():
op.add_column('derived_table', sa.Column('foo', sa.BigInteger(), nullable=True),
schema='tenant_schema')
the run_migrations_online
function:
env.py
schema = 'other_name' # normally i get the name as an argument from alembic
def run_migrations_online():
connectable = create_engine(get_url(), echo=True)
with connectable.connect() as connection:
# setting up the translation map
conn = connection.execution_options(schema_translate_map={'tenant_schema': schema})
context.configure(
connection=conn,
target_metadata=target_metadata,
include_schemas=True,
version_table_schema=schema,
include_object=include_object,
)
with context.begin_transaction():
context.run_migrations()
the exception (full traceback is too long and not that informative):
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) schema "tenant_schema" does not exist
[SQL: 'ALTER TABLE tenant_schema.derived_table ADD COLUMN foo BIGINT']
(Background on this error at: http://sqlalche.me/e/f405)
as you can see it tries to do ALTER TABLE tenant_schema.derived_table
instead of the desired ALTER TABLE other_name.derived_table