Django:遵循一对一关系的相反方向

发布于 2024-12-06 15:17:22 字数 433 浏览 1 评论 0原文

我对 Django 建模一对一关系的方式有疑问。

假设我们有2个模型:A和B:

class B(models.Model):
      bAtt = models.CharField()

class A(models.Model):
      b = models.OneToOneField(B)

在创建的表A中,有一个字段“b_id”,但在Django创建的表B中没有“a_id”字段。

因此,给定一个 A 对象,只需通过 A 行的“b_id”列即可检索相应的 B 对象,这当然很快。

但是 Django 如何在给定 B 对象的情况下检索 A 对象呢?

最坏的情况是扫描A表以在“b_id”列中搜索B.id。如果是这样,我们是否建议在B模型和表中手动引入一个额外的字段“a_id”?

提前致谢!

I have a question about the way Django models the one-to-one-relationship.

Suppose we have 2 models: A and B:

class B(models.Model):
      bAtt = models.CharField()

class A(models.Model):
      b = models.OneToOneField(B)

In the created table A, there is a field "b_id" but in the table B created by Django there is no such field as "a_id".

So, given an A object, it's certainly fast to retrieve the corresponding B object, simply through the column "b_id" of the A's row.

But how does Django retrieve the A object given the B object?

The worst case is to scan through the A table to search for the B.id in the column "b_id". If so, is it advisable that we manually introduce an extra field "a_id" in the B model and table?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記憶穿過時間隧道 2024-12-13 15:17:22

将 id 存储在其中一张表中就足够了。

在情况 1 中,(ab) 生成的 SQL 为

SELECT * from B where b.id = a.b_id

: 在情况 2 中,生成的 SQL 为:

SELECT * from A where a.b_id = b.id

您可以看到,两种方式生成的 SQL 都很相似,并且分别取决于它们的大小。

几乎在所有情况下,索引就足够了,并且只需 1 个 id 即可获得良好的性能。

Storing the id in one of the tables is enough.

In case 1, (a.b) the SQL generated would be

SELECT * from B where b.id = a.b_id

and in case 2, the SQL would be:

SELECT * from A where a.b_id = b.id

You can see that the SQLs generated either way are similar and depend respectively on their sizes.

In almost all cases, indexing should suffice and performance would be good with just 1 id.

紫竹語嫣☆ 2024-12-13 15:17:22

其他对象关系映射器要求您定义双方的关系。 Django 开发人员认为这违反了 DRY(不要重复自己)原则,因此 Django 仅要求您在一端定义关系。

请务必查看文档;)

Other object-relational mappers require you to define relationships on both sides. The Django developers believe this is a violation of the DRY (Don't Repeat Yourself) principle, so Django only requires you to define the relationship on one end.

Always take a look at the doc ;)

白首有我共你 2024-12-13 15:17:22

Django 通过 SQL 中的 JOIN 检索字段,有效地将两个表中 b_id 与 B.id 匹配的行融合在一起。

假设您有以下表格:

# Table B
| id | bAtt    |
----------------
|  1 | oh, hi! |
|  2 | hello   |

# Table A
| id | b_id |
-------------
| 3  |  1   |
| 4  |  2   |

然后 B.id = b_id 上的联接将创建以下元组:

| B.id | B.bAtt  | A.id |
------------------------
|  1   | oh, hi! |  3   |
|  2   | hello   |  4   |

无论您从哪一侧输入关系,Django 都会执行此操作,因此同样有效:) 如何数据库实际上执行连接取决于数据库实现,但以一种或另一种方式,它必须遍历每个表中的每个元素并比较连接属性。

Django retrieves the field through a JOIN in SQL, effectively fusing the rows of the two tables together where the b_id matches B.id.

Say you have the following tables:

# Table B
| id | bAtt    |
----------------
|  1 | oh, hi! |
|  2 | hello   |

# Table A
| id | b_id |
-------------
| 3  |  1   |
| 4  |  2   |

Then a join on B.id = b_id will create the following tuples:

| B.id | B.bAtt  | A.id |
------------------------
|  1   | oh, hi! |  3   |
|  2   | hello   |  4   |

Django does this no matter which side you enter the relation from, and is thus equally effective :) How the database actually does the join depends on the database implementation, but in one way or another it has to go through every element in each table and compare the join attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文