[英]Can two models belong_to each other?
I have two models which are one-to-one to each other. 我有两个彼此一对一的模型。 A currently has one B.
A当前有一个B。
Lately I encounter many cases where it is desirable if A keeps the id of B in order to simplify logic and boost performance. 最近,我遇到许多情况,为了简化逻辑并提高性能,A希望保留B的ID。 However I wonder if:
但是我想知道是否:
UPDATE 更新
I was wrong, the left outer join would not be benefited by the extra foreign key. 我错了,额外的外键不能使左外部联接受益。
The only place I can think of is to find all A's which does not have B, an inner join is required on each of my 100000+ records. 我能想到的唯一地方是找到所有没有B的A,在我的100000+条记录中的每条都需要一个内部联接。 But if there is an id then I can know straight away which A has a B.
但是,如果有一个ID,那么我马上就能知道哪个A有一个B。
I don't believe this is possible - you have to decide where to keep the foreign key. 我认为这是不可能的-您必须决定在哪里保留外键。 Would it make sense for you to use a joining through relationship and for both your existing models to be has_one through the join?
对您而言,使用通过关联关系进行连接,并且将现有的两个模型都通过连接进行为has_one有意义吗?
Another alternative is to put A id on B, ie denormalize. 另一种选择是将A id放在B上,即反规范化。 This would allow you to figure out which A's without a B, and A's with a B. This might be appropriate for reporting scenarios when B's don't move between A's often.
这将使您找出哪个A不带B,一个A不带B。这可能适用于报告B不经常在A之间移动的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.