Flask-Sqlalchemy访问模式是否不同? [英] Flask-Sqlalchemy Acess different schema?
问题描述
说我有一个Postgres数据库,其中有两个不同的架构,一个叫做public
,另一个叫做development
.我知道在类级别上可以为每个模型设置__table_args__ = {"schema":"schema_name"}
,但是如果我想从开发切换到产品.我不会更新所有模型吗?
Say I have a Postgres database with two different schema's, one called public
and one called development
. I know at the class level I can set __table_args__ = {"schema":"schema_name"}
for each model, but if I wanted to switch from development to prod. wouldn't I haven't to update all my models?
是否可以在flask中为dev和prod模式设置模式?还是应该将另一个数据库创建为开发数据库,然后将prod数据库备份到开发数据库?
Is there a way to set the schema for the dev and prod schemas in flask? Or should I just create another database as a dev database and just backup the prod database to the development database?
推荐答案
假设配置中沿着PRODUCTION = False/True的内容,那么您应该可以通过添加到模型中来设置模式:>
Assuming there's something in the config along the lines of PRODUCTION = False/True, then you should be able to set the schema by adding into the models:
if app.config['PRODUCTION']:
db_schema = 'public'
else:
db_schema = 'development'
然后更新您的table_args:
And then update your table_args:
__table_args__ = {'schema': db_schema }
这篇关于Flask-Sqlalchemy访问模式是否不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!