远程端和本地端有什么区别?
给定一个模型:
class Parent(Base):
# ...
id = Column(Integer, primary_key=True)
children = relationship("Child")
class Child(Base):
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
关于关系Parent.children,Parent 上的列是local 端,Child 上的列是远程端。
这似乎有点微不足道,只有当你有所谓的“自引用”关系时才会变得有趣,双方都引用同一张表:
class Parent(Base):
# ...
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
children = relationship("Parent")
其中,Parent.id 是 Parent.children 的本地端,Parent.parent_id 是远程端,基于 Parent -> .children -> Parent 考虑左侧是“本地”,右侧是“远程”。
如果有 remote_side 那为什么没有 local_side?p>
有一个本地方面,如果你说 Parent.children.property.local_side 你会看到它。 remote_side 和 local_side 只是关系需要担心的事情,remote_side 是公共的,您可以设置它仅用于通过自引用关系为关系提供提示;没有别的。
在此处给出的示例中,parent_id“本地”端如何?
如果您有Node.parent,则看起来像Node --> .parent --> Node。 “本地”表示左侧,“远程”表示右侧。多对一自引用连接的方式类似于Node.parent_id = Node.id,因此 parent_id 是本地的。
remote_side 接受一个列表,那么该列表的元素是什么
应该是?如果它们是一个以上的元素,那么什么
这究竟意味着什么?
这是一个列表,因为在 SQLAlchemy 中,所有主键和外键都可能是复合的,也就是说,由多个列组成。在代理键的典型情况下,它是一个元素的列表。
总体而言,您永远不需要使用remote_side,除非在非常特殊的多对一自引用关系的情况下。否则永远不需要它。