Flask Sqlalchemy : relationships between different modules

我只是一个虾纸丫 提交于 2019-11-29 07:55:47

You need to have only one set of the below, and not a separate copy for each model:

app = Flask(my_app_name)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///C:\\MyBase\\Base.sqlite'
db = SQLAlchemy(app)

This can be defined in a separate module (lets call it shared), and imported into each model definition file.
In this case the main module will look more like:

from DataBase.Tables.shared import db

if __name__ == "__main__":
    import DataBase.Tables.Person   # will load Person model into the db
    import DataBase.Tables.Address  # will load Address model into the db
    db.create_all() # will create all models
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!