#19073: strange behaviuor of select_related
----------------------------------------------+--------------------
     Reporter:  anonymous                     |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.4
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 Sometimes Django uses inner join instead of left outer join in queries
 with select_related. This breaks queries with nullable OneToOne fields.
 Here's the example

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

 class B(models.Model):
     pass

 class C(models.Model):
     b = models.OneToOneField(B)
     a = models.ForeignKey(A)

 class D(models.Model):
     b = models.OneToOneField(B, null=True)
 }}}

 code
 {{{
 D.objects.all()
 [<D: D object>, <D: D object>, <D: D object>] #ok
 >>> D.objects.select_related('b').all()
 [<D: D object>, <D: D object>, <D: D object>] #ok
 >>> D.objects.select_related('b', 'b__c').all()
 [<D: D object>, <D: D object>, <D: D object>] #ok
 >>> D.objects.select_related('b', 'b__c__a').all()
 [] #WRONG! empty result
 >>> print D.objects.select_related('b', 'b__c__a').all().query
 SELECT "app1_d"."id", "app1_d"."b_id", "app1_b"."id", "app1_c"."id",
 "app1_c"."b_id", "app1_c"."a_id", "app1_a"."id" FROM "app1_d" LEFT OUTER
 JOIN "app1_b" ON ("app1_d"."b_id" = "app1_b"."id") LEFT OUTER JOIN
 "app1_c" ON ("app1_b"."id" = "app1_c"."b_id") INNER JOIN "app1_a" ON
 ("app1_c"."a_id" = "app1_a"."id")

 }}}

 '''INNER JOIN "app1_a"''' should be '''LEFT OUTER JOIN "app1_a"''' imo

-- 
Ticket URL: <https://code.djangoproject.com/ticket/19073>
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 https://groups.google.com/groups/opt_out.


Reply via email to