Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

python - 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 their tutorial.

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    addresses = db.relationship('Address', backref='person',
                                lazy='dynamic')

class Address(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(50))
    person_id = db.Column(db.Integer, db.ForeignKey('person.id'))

After that, I can create the database :

from DataBase.Tables.MyClass import db
db.create_all()

It works well when both classes are created on the same file.

It does not work anymore when I want to create this through 2 different files (2 different modules).

This is just an example (I'm trying to do something much more complicated with plenty of classes and I need the relationship to exist between 2 different modules but I'll simplify it so my question can be easier to understand.)

I have 2 modules : Person and Address, both of them have :

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

and each of them has the declaration of the class written before.

My main function is in a 3rd module:

from DataBase.Tables.Person import db as person_db
from DataBase.Tables.Address import db as address_db

if __name__ == "__main__":
    import DataBase.Tables.Person
    import DataBase.Tables.Address
    person_db.create_all()
    address_db.create_all()

I still get an error in Eclipse:

*sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'Address.person_id' could not find table 'person' with which to generate a foreign key to target column 'sid'*

I could find another post with someone suggesting the use of "metadata" but I couldn't find a proper way to use that.

Does anyone have an idea to solve this ?

Thanks !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...