将 postgresql 模式传递给 alembic 迁移

Pass postgresql schema to alembic migration

我有一个 postgresql 数据库,我想在 DSN 中传递 postgresql 模式名称,如下所示:postgresql://login:password@postgreshost/dbname?schema=my_schema。我知道我可以在 op.create_table 之类的迁移操作中指定 schema 关键字。不幸的是,upgrade()downgrade() 函数没有我可以传递 postgres 模式的参数。有什么方法可以将架构名称传递给 op.create_table() 而无需对其进行硬编码?

如果始终是默认架构,您可以为 Postgres 用户更改 schema search path

alter user the_user set search_path = my_schema;

这将使所有使用非限定标识符的语句默认使用 my_schema