【发布时间】:2019-02-10 18:28:35
【问题描述】:
我有一个适用于名为 tenant_schema 的通用架构的迁移。
在env.py 的run_migrations_online 函数中,我为tenant_schema 设置了一个schema_translate_map。
我希望 sqlalchemy 将此迁移操作转换为在所需架构上运行,但它似乎尝试使用架构 tenant_schema 运行 sql 查询。
任何想法如何解决它?
示例:
迁移文件中的升级功能:
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')
run_migrations_online 函数:
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()
异常(完整回溯太长且信息量不足):
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)
如您所见,它尝试执行 ALTER TABLE tenant_schema.derived_table 而不是所需的 ALTER TABLE other_name.derived_table
【问题讨论】:
标签: python orm sqlalchemy alembic