SQLalchemy中的backref和back_populate概念? [英] Concepts of backref and back_populate in SQLalchemy?

查看:515
本文介绍了SQLalchemy中的backref和back_populate概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释这两个想法的概念,以及它们与表之间的关系如何?我似乎找不到真正能清楚解释它的东西,而且文档觉得简单概念中的术语太多了.例如,在文档中的一对多关系示例中:

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")

为什么 relationship()进入父类,而 ForeignKey 进入子类?而 back_populates 相互之间到底有什么作用?问题中是否存在 relationship()函数属于哪个类?

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?

推荐答案

backref 是用于同时配置 parent.children child.parent relationship 只能在父类或子类上(而不是两者)放在一个位置上.也就是说,不用

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

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

您只需要其中之一:

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

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

children = relationship("Child",backref ="parent")将在子类上自动创建 .parent 关系.另一方面,如果使用 back_populates ,则必须必须在父类和子类中显式创建 relationship .

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.

为什么Relationship()为什么放在父类中而ForeignKey放在子类中?

如上所述,如果您使用 back_populates ,则需要同时使用父类和子类.如果您使用 backref ,则只需继续使用其中之一. ForeignKey 需要继续进行子类,无论 relationship 放在何处,这都是关系数据库的基本概念.

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.

又有back_populates到底是做什么的呢?

back_populates 通知彼此之间的每个关系,以便它们保持同步.例如,如果您这样做

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

如您所见,即使没有明确设置, p1 仍被设置为 c1 的父级.

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

relation()函数在物质中是否位于哪一类的位置?

这仅适用于 backref ,否,您可以将关系放在父类上( children = relationship("Child",backref ="parent"))或子类( parent = Relationship("Parent",backref ="children"))上,效果完全相同.

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.

这篇关于SQLalchemy中的backref和back_populate概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆