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:
- backref
- primaryjoin
- lazy
- uselist
- order_by
- secondary
- secondaryjoin