Flask-Sqlalchemy Acess different schema?

走远了吗. 提交于 2019-12-23 01:07:13

问题


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?

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?


回答1:


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'

And then update your table_args:

__table_args__ = {'schema': db_schema }


来源:https://stackoverflow.com/questions/49409691/flask-sqlalchemy-acess-different-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!