#16715: Wrong JOIN with nested null-able foreign keys
-------------------------------------+-------------------------------------
               Reporter:  sebastian  |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Milestone:             |      Component:  Database layer
                Version:  SVN        |  (models, ORM)
             Resolution:             |       Severity:  Normal
           Triage Stage:  Accepted   |       Keywords:  join, values,
    Needs documentation:  0          |  nested, foreign key, null-able
Patch needs improvement:  1          |      Has patch:  1
                  UI/UX:  0          |    Needs tests:  1
                                     |  Easy pickings:  0
-------------------------------------+-------------------------------------

Comment (by sebastian):

 I am attaching a patch that adds another test to
 `regressiontests/model_inheritance_select_related`. In this test, very
 similar to what I had in comment:2, I basically have the following models:

 {{{
 class Chain(models.Model):
     pass

 class Place(models.Model):
     pass

 class Franchise(Place):
     chain = models.ForeignKey(Chain)
 }}}

 (That is, one multi-table inheritance from Place to Franchise, and one
 non-nullable foreign key from Franchise to Chain.)

 I then create a Place object explicitly (i.e. a place that is not a
 franchise), and a Franchise object (implicitly creating a Place object)
 with an associated Chain object. The following three queries now should
 all return the same queryset consisting of the two Place objects (one for
 the Place, one for the Franchise):

 {{{
 Place.objects.all()
 Place.objects.select_related("franchise")
 Place.objects.select_related("franchise__chain")
 }}}

 However, due to the wrong join being selected between the Franchise and
 Chain model, the third queryset only returns the Place that is also a
 Franchise. In other words, the other Place that was created explicitly
 without a Franchise is now missing.

 When adding an attribute `null=True` to the chain foreign key in
 Franchise, all three querysets return both objects. This is because
 `null=True` forces the correct left outer join between Franchise and
 Chain.

 Regarding the issue with `values` and foreign keys (instead of the multi-
 table inheritance/one-to-one relation used in this test), I guess both
 problems have the some original cause, so I didn't write a test for
 `values` (neither for foreign-key instead of one-to-one relation). Fixing
 the `select_related` issue might also fix the other problem(s). My guess
 is that something deep inside the query generator goes wrong when it makes
 the choice of inner join vs. left outer join on nested foreign-key
 relationships.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/16715#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to