In Flask-SQLAlchemy, the backref parameter in relationship method allows you to declare a new property under a specified class as seen in the example in their docs:
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'))
But then there is also a backref function:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
addresses = db.relationship('Address',
backref=db.backref('person', lazy='joined'),
lazy='dynamic')
In this case, what's the role of the backref function passed to the backref parameter, especially with the multiple lazy definitions? How is it different from backref='person'?
Address.personhas joined loading configured. When you pass just a string, you get the defaults. - Ilja Everilälazy='dynamic'also change the loading configuration? I think this is what is confusing me - kentwaitUser.addresses. - Ilja Everiläbackrefjust automates the creation of a relationship property at the other end.backref='person'is somewhat akin to havingperson = db.relationship('Person')explicitly in the Address class (+ back population). Using thebackref()object you can pass arguments to that relationship. - Ilja Everilä