Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concepts of backref and back_populate in SQLalchemy?

Can anyone explain the concepts of these two ideas and how they relate to making relationships between tables? I can't really seem to find anything that explains it clearly and the documentation feels like there's too much jargon to understand in easy concepts. For instance, in this example of a one to many relationship in the documentation:

class Parent(Base):     __tablename__ = 'parent'     id = Column(Integer, primary_key=True)     children = relationship("Child", back_populates="parent")  class Child(Base):     __tablename__ = 'child'     id = Column(Integer, primary_key=True)     parent_id = Column(Integer, ForeignKey('parent.id'))     parent = relationship("Parent", back_populates="children") 

Why does the relationship() go inside the parent class while ForeignKey goes inside the child class? And what does having back_populates exactly do to one another? Does having the placement of which class the relationship() function exist in matter?

like image 665
BoopityBoppity Avatar asked Jul 14 '18 04:07

BoopityBoppity


People also ask

What is Backref in SQLAlchemy?

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 ...

What is difference between Backref and Back_populates?

back_populates has the same meaning as backref , except that the complementing relationship property is not created automatically. So using back_populates makes the model code more explicit, with no hidden/implicit properties. See the relationship-backref documentation for more information.

Does SQLAlchemy need Backref?

If you use backref you don't need to declare the relationship on the second table. If you're not using backref , and defining the relationship 's separately, then if you don't use back_populates , sqlalchemy won't know to connect the relationships, so that modifying one also modifies the other.

What is Back_populates in SQLAlchemy?

The back_populates argument tells SqlAlchemy which column to link with when it joins the two tables. It allows you to access the linked records as a list with something like Parent.


1 Answers

backref is a shortcut for configuring both parent.children and child.parent relationships at one place only on the parent or the child class (not both). That is, instead of having

children = relationship("Child", back_populates="parent")  # on the parent class 

and

parent = relationship("Parent", back_populates="children")  # on the child class 

you only need one of this:

children = relationship("Child", backref="parent")  # only on the parent class 

or

parent = relationship("Parent", backref="children")  # only on the child class 

children = relationship("Child", backref="parent") will create the .parent relationship on the child class automatically. On the other hand, if you use back_populates you must explicitly create the relationships in both parent and child classes.

Why does the relationship() go inside the parent class while ForeignKey goes inside the child class?

As I said above, if you use back_populates, it needs to go on both parent and child classes. If you use backref, it needs to go on one of them only. ForeignKey needs to go on the child class, no matter where the relationship is placed, this is a fundamental concept of relational databases.

And what does having back_populates exactly do to one another?

back_populates informs each relationship about the other, so that they are kept in sync. For example if you do

p1 = Parent() c1 = Child() p1.children.append(c1) print(p1.children)  # will print a list of Child instances with one element: c1 print(c1.parent)  # will print Parent instance: p1 

As you can see, p1 was set as parent of c1 even when you didn't set it explicitly.

Does having the placement of which class the relationship() function exist in matter?

This only applies to backref, and no, you can place the relationship on the parent class (children = relationship("Child", backref="parent")) or on the child class (parent = relationship("Parent", backref="children")) and have the exact same effect.

like image 188
eRunner Avatar answered Oct 10 '22 04:10

eRunner