2024-05-18 15:33:17 发布
网友
我已经建立了两个flask应用程序,一个用于商业(应用程序1),另一个用于管理(应用程序2)。应用程序1和应用程序2共享相同的数据库和表,但应用程序2使用的订阅相关表很少,而这些表在应用程序1中不存在。现在的问题是,每当我在应用程序1中执行SQL Alchemy migrate命令时,它都会为订阅表生成drop table语法,因为这些表模型在应用程序1中不存在
使用SQL Alchemy migrate处理共享同一数据库的两个flask应用程序的最佳方法是什么
可以在app1的alembic.ini文件中定义要在app1中排除的表,示例如下:
[alembic:exclude] tables = table1,table2
然后在env.py文件中,可以使用include_对象函数
include_object是一个可调用函数,它有机会为任何对象返回True或False,指示是否应在自动生成扫描中考虑给定对象
https://alembic.sqlalchemy.org/en/latest/api/runtime.html?highlight=include_object#alembic.runtime.environment.EnvironmentContext.configure.params.include_object
env.py文件中的代码
exclude_tables = config.get_section('alembic:exclude').get('tables', '').split(',') def include_object(object, name, type_, reflected, compare_to): if type_ == "table" and name in exclude_tables: return False else: return True
exclude_tables将包含您在alembic.ini文件中定义的所有表的列表
然后在您的上下文中包含此include_对象函数。按如下所示配置函数:
context.configure( # ... include_object = include_object )
可以在app1的alembic.ini文件中定义要在app1中排除的表,示例如下:
然后在env.py文件中,可以使用include_对象函数
include_object是一个可调用函数,它有机会为任何对象返回True或False,指示是否应在自动生成扫描中考虑给定对象
https://alembic.sqlalchemy.org/en/latest/api/runtime.html?highlight=include_object#alembic.runtime.environment.EnvironmentContext.configure.params.include_object
env.py文件中的代码
exclude_tables将包含您在alembic.ini文件中定义的所有表的列表
然后在您的上下文中包含此include_对象函数。按如下所示配置函数:
相关问题 更多 >
编程相关推荐