vendredi 23 janvier 2015

SQLAlchemy relationships, when to use which relationship?

I have just started learning Flask and using the flask-sqlalchemy package. I have a schema where roles(id, name) and users(id, username, password, role_id) and role_id is a foreign key for roles.id. One to many relationship as one role can be assigned to many users but every user will have a single role.


In the following code,



class Role(db.Model):
# ..
users = db.relationship('User', backref='role')

class User(db.Model):
# ..
role_id = db.Column(db.Integer, db.ForeignKey('roles.id')


Here, I have established the usage of role_id, but why do we need db.relationship? What is the use of the relationship options and where to use what:



  1. backref

  2. primaryjoin

  3. lazy

  4. uselist

  5. order_by

  6. secondary

  7. secondaryjoin


Aucun commentaire:

Enregistrer un commentaire