Hello, I'd like to draw your attention to ticket #16715:
"Wrong JOIN with nested null-able foreign keys" https://code.djangoproject.com/ticket/16715 It seems that the Django query generator sometimes picks the wrong join type in the following situation: Model A | (Relation A/B: null=True) v Model B | (Relation B/C: null=False) v Model C A priori, the correct/default join for Relation A/B is LEFT OUTER JOIN, while the correct join for Relation B/C is INNER JOIN. However, when combining all three models, the join between Model B and C must be promoted to LEFT OUTER JOIN. If we wouldn't do that, instances of Model A that do not reference an instance of Model B would be missing in the resulting queryset: the INNER JOIN wouldn't be able find the matching Model C with a primary key of NULL which results from the join between Models A and B for As without referenced Bs. This promotion works in most cases and is done in django/db/models/sql/query.py, methods 'promote_alias' and 'promote_alias_chain' of class Query. However, the promotion fails, and Django falls back to the wrong INNER JOIN for Relation B/C in at least some cases involving 'select_related' and 'values'. In those cases the relation between A and B can either be an explicit foreign key (A -> B) or an implicit/reverse one such as introduced by one-to-one relations (e.g. in multi-table inheritance). I outlined those cases in #16715. I also provided two semi-elaborate tests for my observation ("wrong-join-test-r16910.patch" and "nested-foreign-key-test-r16914.patch"). What do you think? Is this something that can be fixed easily? My fears are that this points to some deeper problem within the query generator, in which the generator fails to correctly promote the join type for the nested relation. Sebastian. -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.