简体   繁体   English

了解SQLAlchemy ForeignKey 关系查询结果

[英]Understanding SQLAlchemy ForeignKey Relationship query results

I'm trying to understand how SQLAlchemy query output can be continuously nested as if its an infinite object.我试图了解 SQLAlchemy 查询 output 如何可以连续嵌套,就好像它是一个无限的 object。 (I sure this is not the right way to call it) (我确定这不是正确的称呼方式)

The scenario is as below.场景如下。 I have created a one-to-many relationship between a user and their pets.我在用户和他们的宠物之间创建了一对多的关系。

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True)

    pets = relationship("Pet", back_populates="owner")

class Pet(Base):
    __tablename__ = "pets"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    owner_id = Column(Integer, ForeignKey("users.id"))

    owner = relationship("User", back_populates="pets")

Now, when I query for the User, I could also query for the pets information as seen below:现在,当我查询用户时,我还可以查询宠物信息,如下所示:

records = session.query(User).all()

This returns a list of users with their pets in the imaginary field.这将返回一个用户列表,他们的宠物在虚构的领域中。
Now when I try to get access to the pets information, I can do so by accessing the array.现在,当我尝试访问宠物信息时,我可以通过访问数组来实现。 For example:例如:

records[0].pets[0].name

At the same time, I can continuously access the user again, and then the number of pets and the user again and so on...同时我可以不断地再次访问用户,然后再次访问宠物数量和用户等等……

records[0].pets[0].owner.pets[0].owner.pets[0].....

This question was raised when I was exploring graphQL and encountered something similar.这个问题是我在探索 graphQL 时提出的,遇到了类似的情况。

My question is, how is this phenomena made possible in Python?我的问题是,这种现象是如何在 Python 中实现的? Is this an infinite object or is there some object circular referencing thing?这是无限的 object 还是有一些 object 循环引用的东西? I am sorry I could not put this in the right wording as I am unsure what is this problem/ feature called.很抱歉,我无法用正确的措辞来说明这个问题,因为我不确定这个问题/功能是什么。

Thank you all very much for your time and have a pleasant day.非常感谢大家的宝贵时间,祝您有愉快的一天。

Regards,问候,
Justin贾斯汀

EDIT:编辑:
Found the docs that explains this!找到解释这一点的文档!
https://docs.sqlalchemy.org/en/13/orm/self_referential.html https://docs.sqlalchemy.org/en/13/orm/self_referential.html

I can see how the query in question may appear to be some sort of infinite chain of objects, but as you've pointed out yourself, this is perhaps best understood as a form of circular reference.我可以看到有问题的查询可能看起来是某种无限的对象链,但正如您自己指出的那样,这可能最好理解为循环引用的一种形式。

More concretely, each pet object can have one owner.更具体地说,每只宠物 object 可以有一个主人。 In turn, each owner can have multiple pets.反过来,每个主人可以拥有多只宠物。 What the query查询什么

records[0].pets[0].owner.pets[0].owner.pets[0].....

is doing is that you're getting the owner of the first pet, then the pets of that owner, then the owner of the first pet, and so forth.正在做的是你得到第一个宠物的主人,然后是那个主人的宠物,然后是第一个宠物的主人,等等。

We can thus establish the following recurrence relationship as well.因此,我们也可以建立以下递归关系。

records[0].pets[0] = records[0].pets[0].owner.pets[0]

So the infinite chain is, under the hood, not truly infinite;因此,在引擎盖下,无限链并不是真正的无限。 the query is simply going in circles.查询只是绕圈子。 @Klaus D. has already provided an excellent analogy of a front door. @Klaus D. 已经提供了一个很好的前门类比。 I might add in here another potentially intuitive reference in CS, which is a tree data structure: you're moving from one leaf to its parent node, then back down to the leaf.我可能会在此处添加 CS 中另一个可能直观的参考,它是一种树数据结构:您正在从一个叶子移动到它的父节点,然后返回到叶子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM