在单个数据库上使用 flask-migrate 创建多个具有不同模式的 table
create multiple table with different schema using flask-migrate on a single database
我正在使用 flask-migrate 来处理 table 的创建和维护。我有多个具有不同架构的 table。
class A(db.model):
__tablename__ = 'A'
__table_args__ = {'schema': 'public'}
# rest of data
class B(db.model):
__tablename__ = 'B'
__table_args__ = {'schema': 'schema_b'}
# rest of data
所以当我运行flask db init
和flask db migrate
时,迁移文件夹中创建了一个迁移脚本。但是当我 运行 flask db upgrade
在数据库中添加 table 时,它显示错误
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidSchemaName) schema "schema_b.B" does not exist
当我搜索这个问题时,我发现了这个include_schemas and migrate using different schema,在这两个部分中都提到在migration/env.py
中使用configure
中的include_schemas=True
。另外,答案中解决方案中提到的 link 无效 link,所以这对我来说变得有点程序化了。
我已经做了相应的修改。然后我是 运行 flask db migrate
,它正在检测具有所有模式的所有 table。但正如我 运行 flask db upgrate
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidSchemaName) schema "schema_b.B" does not exist
再次出现错误。
帮帮我,如何使用flask migrate
解决这个问题。要创建一个 table 我有一个工作正常的 SQL 命令。
include_schemas=True
选项使 Alembic 在您的非默认模式中查找 table,但当您将它们添加到模型定义时它无法生成新模式。
在这种情况下,您要做的是运行 flask db migrate
生成迁移,然后打开生成的迁移脚本并在新[=24]之前添加架构创建=] 被创建。例如,在您问题的示例中,您的 B
模型的迁移或多或少会像这样生成:
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('schema_b.B',
# ...
)
# ### end Alembic commands ###
所以需要在table创建上面添加schema创建语句,这样schema在table创建的时候就已经存在了:
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.execute('create schema schema_b') # <--- add this
op.create_table('schema_b.B',
# ...
)
# ### end Alembic commands ###
然后为了保持一致性,还将架构删除到降级路径中:
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('schema_b.B')
op.execute('drop schema schema_b') # <--- add this
# ### end Alembic commands ###
我正在使用 flask-migrate 来处理 table 的创建和维护。我有多个具有不同架构的 table。
class A(db.model):
__tablename__ = 'A'
__table_args__ = {'schema': 'public'}
# rest of data
class B(db.model):
__tablename__ = 'B'
__table_args__ = {'schema': 'schema_b'}
# rest of data
所以当我运行flask db init
和flask db migrate
时,迁移文件夹中创建了一个迁移脚本。但是当我 运行 flask db upgrade
在数据库中添加 table 时,它显示错误
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidSchemaName) schema "schema_b.B" does not exist
当我搜索这个问题时,我发现了这个include_schemas and migrate using different schema,在这两个部分中都提到在migration/env.py
中使用configure
中的include_schemas=True
。另外,答案中解决方案中提到的 link 无效 link,所以这对我来说变得有点程序化了。
我已经做了相应的修改。然后我是 运行 flask db migrate
,它正在检测具有所有模式的所有 table。但正如我 运行 flask db upgrate
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidSchemaName) schema "schema_b.B" does not exist
再次出现错误。
帮帮我,如何使用flask migrate
解决这个问题。要创建一个 table 我有一个工作正常的 SQL 命令。
include_schemas=True
选项使 Alembic 在您的非默认模式中查找 table,但当您将它们添加到模型定义时它无法生成新模式。
在这种情况下,您要做的是运行 flask db migrate
生成迁移,然后打开生成的迁移脚本并在新[=24]之前添加架构创建=] 被创建。例如,在您问题的示例中,您的 B
模型的迁移或多或少会像这样生成:
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('schema_b.B',
# ...
)
# ### end Alembic commands ###
所以需要在table创建上面添加schema创建语句,这样schema在table创建的时候就已经存在了:
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.execute('create schema schema_b') # <--- add this
op.create_table('schema_b.B',
# ...
)
# ### end Alembic commands ###
然后为了保持一致性,还将架构删除到降级路径中:
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('schema_b.B')
op.execute('drop schema schema_b') # <--- add this
# ### end Alembic commands ###