Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 965
Yace Avatar asked Jul 30 '12 11:07

Yace


People also ask

How do you create a one to many relationship in Flask-SQLAlchemy?

The comments class attribute defines a One-to-Many relationship between the Post model and the Comment model. You use the db. relationship() method, passing it the name of the comments model ( Comment in this case). You use the backref parameter to add a back reference that behaves like a column to the Comment model.

How do I create a many to many relationship in SQLAlchemy?

Many to Many relationship between two tables is achieved by adding an association table such that it has two foreign keys - one from each table's primary key.

Is there something better than SQLAlchemy?

Django, Pandas, Entity Framework, peewee, and MySQL are the most popular alternatives and competitors to SQLAlchemy.


1 Answers

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
like image 133
van Avatar answered Sep 20 '22 17:09

van