Skip to content Skip to sidebar Skip to footer

Flask Sqlalchemy : Relationships Between Different Modules

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' relationship, like in

Solution 1:

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 dbimport DataBase.Tables.Address  # will load Address model into the db
    db.create_all() # will create all models

Post a Comment for "Flask Sqlalchemy : Relationships Between Different Modules"