Cache back-references on OneToOne fields
|Reported by:||Shai Berger||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Today, back-references on OneToOneField's are not cached. In more detail, assume:
class A(model): pass class B(model): a = OneToOneField(A) a1 = A.objects.get(...) # assume a1 has a related B record b1 = B.objects.get(...)
Today, each of the expressions
b1.a.b generates two database queries, and as a result, both
(a1.b.a is a1) and
(b1.a.b is b1) are false. This is surprising and can be both a performance issue and a correctness issue -- therefore classified as "Bug", although it is borderline "optimization".
(Of course, nobody in their right mind actually writes code like
a1.b.a; the issue arises e.g. by function
a1 and calling
g does something with
Attached is a patch with tests that failed on trunk r17377.