Flask Sqlalchemy : relationships between different modules

前端 未结 1 1105
心在旅途
心在旅途 2020-12-18 04:53

I\'m following the Flask-SQLAlchemy tutorial. I have Flask 0.9, sqlalchemy 0.7.8 and flask-sqlalchemy 0.16 on python 2.6.

I\'m trying to create a \"one to many\" rel

相关标签:
1条回答
  • 2020-12-18 05:29

    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
    
    0 讨论(0)
提交回复
热议问题