Boo*_*ity 7 python database sqlalchemy
谁能解释这两个想法的概念,以及它们与表之间的关系如何?我似乎真的找不到任何能清楚解释它的内容,并且文档觉得简单概念中的术语太多了。例如,在文档中的一对多关系示例中:
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")
Run Code Online (Sandbox Code Playgroud)
为什么relationship()
进入父类而ForeignKey
进入子类?back_populates
彼此之间到底有什么作用?relationship()
函数在哪个类中是否存在?
小智 52
backref
是一种快捷方式,用于仅在父类或子类(而不是两者)的一个地方配置parent.children
和child.parent
relationship
s。也就是说,而不是拥有
children = relationship("Child", back_populates="parent") # on the parent class
Run Code Online (Sandbox Code Playgroud)
和
parent = relationship("Parent", back_populates="children") # on the child class
Run Code Online (Sandbox Code Playgroud)
你只需要其中之一:
children = relationship("Child", backref="parent") # only on the parent class
Run Code Online (Sandbox Code Playgroud)
或者
parent = relationship("Parent", backref="children") # only on the child class
Run Code Online (Sandbox Code Playgroud)
children = relationship("Child", backref="parent")
将.parent
自动在子类上创建关系。在另一方面,如果你使用back_populates
,你必须明确地建立relationship
在两家母公司和子类秒。
为什么relationship() 在父类里面,而ForeignKey 在子类里面?
正如我上面所说的,如果你使用back_populates
,它需要在父类和子类中进行。如果您使用backref
,则只需要使用其中之一。ForeignKey
需要上子类,不管relationship
放在哪里,这是关系数据库的一个基本概念。
back_populates 到底对彼此有什么影响?
back_populates
通知每个关系关于另一个,以便他们保持同步。例如,如果你这样做
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
Run Code Online (Sandbox Code Playgroud)
如您所见,即使您没有明确设置它,它也p1
被设置为父级c1
。
关系()函数存在于哪个类的位置是否重要?
这仅适用于backref
,不可以,您可以将关系放在父类 ( children = relationship("Child", backref="parent")
) 或子类 ( parent = relationship("Parent", backref="children")
) 上并具有完全相同的效果。
u2g*_*les 15
显然,应首选使用back_populates
显式relationship()
构造,如下所示:
https://docs.sqlalchemy.org/en/14/orm/backref.html
归档时间: |
|
查看次数: |
1600 次 |
最近记录: |