Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 373 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concepts of backref and back_populate in SQLalchemy?

#1
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?


Reply

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

```python
children = relationship("Child", back_populates="parent") # on the parent class
```
and
```python
parent = relationship("Parent", back_populates="children") # on the child class
```
you only need one of this:
```python
children = relationship("Child", backref="parent") # only on the parent class
```
or
```python
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 `relationship`s 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
```python
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.
Reply

#3
Apparently, use of `back_populates` with explicit `relationship()` constructs should be preferred as explained at:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through