什么是本地端和远程端

什么是本地端和远程端

本文介绍了什么是本地端和远程端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题与数据库表关系以及 SQLAlchemy 为其提供的抽象相关.

The questions below are related to database table relationships and the abstractions which SQLAlchemy provides for it.

  1. 远程端和本地端有什么区别?
  2. 如果有 remote_side 那么为什么没有 local_side?
  3. 在给定的示例中这里 parent_id 怎么样 local"端?
  4. remote_side 接受一个 list 那么它的元素是什么list 应该是?如果它们的元素多于一个,那么这究竟意味着什么?
  1. What is the difference between remote and local side?
  2. If there is remote_side then why not a local_side?
  3. In the example given here how is parent_id "local" side?
  4. remote_side takes in a list so what are the elements of thatlist supposed to be? And if their are more then one elements thenwhat exactly does that signify?

我已多次阅读文档,但无法理解它背后的基本概念以及如何正确使用它.(几乎)我所知道的是它应该将一对多关系转换为多对一.通常当我尝试使用它时,我感觉它是相关的,我结束引入 SQLAlchemy 抱怨的歧义,在大多数情况下通过删除 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.

推荐答案

给定一个模型:

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.childrenParent 上的列是 local 端, 上的列孩子是远端.

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.

这看起来有点微不足道,只有当你有所谓的自我参照"关系时才会变得有趣,其中双方都引用同一张表:

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

上面,Parent.idParent.children 的本地端,Parent.parent_id 是远程端,基于 父级->.儿童 ->父认为左侧是本地",右侧是远程".

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

如果有 remote_side 那么为什么没有 local_side?

有一个本地端,如果你说 Parent.children.property.local_side 你会看到它.remote_sidelocal_side 只是关系需要担心的事情,而 remote_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_id本地"端如何?

如果你有 Node.parent,它看起来像 Node -->.parent -->节点.本地"表示左侧,远程"表示右侧.多对一自引用连接的方式类似于 Node.parent_id = Node.id,所以 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.

remote_side 接收一个列表,那么该列表的元素是什么应该是?如果他们的元素不止一个,那又是什么这到底是什么意思?

这是一个列表,因为在 SQLAlchemy 中,所有主键和外键都可能是复合的,也就是说,由不止一列组成.在代理键的典型情况下,它是一个包含一个元素的列表.

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.

总的来说,您永远不需要使用 remote_side,除非是多对一的自引用关系的非常特殊的情况.否则永远不需要它.

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.

这篇关于什么是本地端和远程端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:43