﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17565	Cache back-references on OneToOne fields	Shai Berger	nobody	"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 `a1.b.a` and `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 `f` taking `a1` and calling `g(a1.b)`, where `g` does something with `b.a`).

Attached is a patch with tests that failed on trunk r17377.
"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed	OneToOneField, OneToOne		Accepted	1	0	0	0	0	0
