The questions below are related to database table relationships and the abstractions which SQLAlchemy provides for it.
下面的问题与数据库表关系和SQLAlchemy为其提供的抽象有关。
- What is the difference between remote and local side?
- If there is
remote_side
then why not alocal_side
? - In the example given here how is
parent_id
"local" side? -
remote_side
takes in alist
so what are the elements of thatlist
supposed to be? And if their are more then one elements then what exactly does that signify?
远程和本地端有什么区别?
如果有remote_side那么为什么不是local_side?
在这里给出的例子中,parent_id是如何“本地”的?
remote_side接受一个列表,那么该列表的元素应该是什么?如果他们是一个元素,那么这究竟意味着什么呢?
I have read the docs several time but fail to understand the basic concept behind it and how to use it appropriately. (Almost) All I know is that it is supposed to convert a one-to-many relationship into many-to-one. And usually when I try to use it, where I feel its relevant, I end introducing ambiguities which SQLAlchemy complains about, which in majority of the cases is fixed by removing the remote_side
argument all together.
我已经多次阅读过这些文档但却无法理解它背后的基本概念以及如何正确使用它。 (差不多)我所知道的是它应该将一对多关系转换为多对一关系。通常当我尝试使用它时,我感觉它的相关性,我最后引入了SQLAlchemy抱怨的歧义,在大多数情况下,通过一起删除remote_side参数来解决这个问题。
1 个解决方案
#1
11
What is the difference between remote and local side?
远程和本地端有什么区别?
given a model like:
给出一个如下模型:
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'))
Regarding the relationship Parent.children
, columns that are present on Parent
are the local
side, columns that are present on Child
are the remote side.
关于Parent.children的关系,Parent上存在的列是本地端,Child上存在的列是远程端。
This seems a bit trivial, and only becomes something interesting when you have a so-called "self-referential" relationship, where both sides refer to the same table:
这看起来有点微不足道,只有当你有一个所谓的“自我指涉”关系时才会变得有趣,双方都在同一张桌子上:
class Parent(Base):
# ...
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
children = relationship("Parent")
Where above, Parent.id
is the local side of Parent.children
and Parent.parent_id
is the remote side, based on Parent -> .children -> Parent
considering the left side to be "local" and the right side to be "remote".
上面的地方,Parent.id是Parent.children的本地端,Parent.parent_id是远程端,基于Parent - > .children - > Parent,将左侧视为“本地”,将右侧视为“远程” ”。
If there is remote_side then why not a local_side?
如果有remote_side那么为什么不是local_side?
There is a local side, if you were to say Parent.children.property.local_side you'd see it. remote_side
and local_side
are only things that the relationship needs to worry about, and remote_side
is public as something you can set only for the purposes of giving relationship a hint with a self referential relationship; nothing else.
有一个本地方面,如果你说Parent.children.property.local_side你会看到它。 remote_side和local_side只是关系需要担心的事情,而remote_side是公开的,你可以设置的东西只是为了给关系提供一个自我引用关系的提示;没有其他的。
In the example given here how is parent_id "local" side?
在这里给出的例子中,parent_id是如何“本地”的?
If you have Node.parent
, that looks like Node --> .parent --> Node
. "local" means the left side and "remote" is the right. The way a many-to-one self referential joins is like Node.parent_id = Node.id
, so parent_id is local.
如果你有Node.parent,它看起来像Node - > .parent - > Node。 “local”表示左侧,“remote”表示右侧。多对一自引用连接的方式类似于Node.parent_id = Node.id,因此parent_id是本地的。
remote_side takes in a list so what are the elements of that list supposed to be? And if their are more then one elements then what exactly does that signify?
remote_side接受一个列表,那么该列表的元素应该是什么?如果他们是一个元素,那么这究竟意味着什么呢?
It's a list because in SQLAlchemy all primary and foreign keys can potentially be composite, meaning, consists of more than one column. In the typical case of a surrogate key, it's a list of one element.
这是一个列表,因为在SQLAlchemy中,所有主键和外键都可能是复合的,这意味着包含多个列。在代理键的典型情况下,它是一个元素的列表。
Overall, you should never need to use remote_side
except in the very specific case of a self-referential relationship that is many-to-one. Otherwise it should never be needed.
总的来说,除了在多对一的自引用关系的特定情况下,你永远不需要使用remote_side。否则永远不需要它。
#1
11
What is the difference between remote and local side?
远程和本地端有什么区别?
given a model like:
给出一个如下模型:
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'))
Regarding the relationship Parent.children
, columns that are present on Parent
are the local
side, columns that are present on Child
are the remote side.
关于Parent.children的关系,Parent上存在的列是本地端,Child上存在的列是远程端。
This seems a bit trivial, and only becomes something interesting when you have a so-called "self-referential" relationship, where both sides refer to the same table:
这看起来有点微不足道,只有当你有一个所谓的“自我指涉”关系时才会变得有趣,双方都在同一张桌子上:
class Parent(Base):
# ...
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
children = relationship("Parent")
Where above, Parent.id
is the local side of Parent.children
and Parent.parent_id
is the remote side, based on Parent -> .children -> Parent
considering the left side to be "local" and the right side to be "remote".
上面的地方,Parent.id是Parent.children的本地端,Parent.parent_id是远程端,基于Parent - > .children - > Parent,将左侧视为“本地”,将右侧视为“远程” ”。
If there is remote_side then why not a local_side?
如果有remote_side那么为什么不是local_side?
There is a local side, if you were to say Parent.children.property.local_side you'd see it. remote_side
and local_side
are only things that the relationship needs to worry about, and remote_side
is public as something you can set only for the purposes of giving relationship a hint with a self referential relationship; nothing else.
有一个本地方面,如果你说Parent.children.property.local_side你会看到它。 remote_side和local_side只是关系需要担心的事情,而remote_side是公开的,你可以设置的东西只是为了给关系提供一个自我引用关系的提示;没有其他的。
In the example given here how is parent_id "local" side?
在这里给出的例子中,parent_id是如何“本地”的?
If you have Node.parent
, that looks like Node --> .parent --> Node
. "local" means the left side and "remote" is the right. The way a many-to-one self referential joins is like Node.parent_id = Node.id
, so parent_id is local.
如果你有Node.parent,它看起来像Node - > .parent - > Node。 “local”表示左侧,“remote”表示右侧。多对一自引用连接的方式类似于Node.parent_id = Node.id,因此parent_id是本地的。
remote_side takes in a list so what are the elements of that list supposed to be? And if their are more then one elements then what exactly does that signify?
remote_side接受一个列表,那么该列表的元素应该是什么?如果他们是一个元素,那么这究竟意味着什么呢?
It's a list because in SQLAlchemy all primary and foreign keys can potentially be composite, meaning, consists of more than one column. In the typical case of a surrogate key, it's a list of one element.
这是一个列表,因为在SQLAlchemy中,所有主键和外键都可能是复合的,这意味着包含多个列。在代理键的典型情况下,它是一个元素的列表。
Overall, you should never need to use remote_side
except in the very specific case of a self-referential relationship that is many-to-one. Otherwise it should never be needed.
总的来说,除了在多对一的自引用关系的特定情况下,你永远不需要使用remote_side。否则永远不需要它。