#18583: exclude generates wrong SQL query
-------------------------------------+-------------------------------------
     Reporter:  rbreu                |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.4
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:  query exclude sql    |      Needs documentation:  0
    Has patch:  0                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by fgallina):

 This bug is invalid, as the generated SQL is correct.

 Given this full example:

     #models
     from django.utils.encoding import python_2_unicode_compatible
     from django.contrib.auth.models import Group


     # Models for #19659
     @python_2_unicode_compatible
     class A(models.Model):
         pass

         def __str__(self):
             return str(self.pk)

     @python_2_unicode_compatible
     class B(models.Model):
         a = models.ForeignKey(A)

         def __str__(self):
             return str(self.pk)

     @python_2_unicode_compatible
     class C(models.Model):
         b = models.ForeignKey(B)

         def __str__(self):
             return str(self.pk)

     @python_2_unicode_compatible
     class D(models.Model):
         a = models.ForeignKey(A)
         group = models.ForeignKey(Group)

         def __str__(self):
             return str(self.pk)

 The following query:

     C.objects.exclude(b__a__d__group__in=[foo]).order_by('pk')

 Results in:

     SELECT "queries_c"."id", "queries_c"."b_id" FROM "queries_c"
        INNER JOIN "queries_b" ON ("queries_c"."b_id" = "queries_b"."id")
        WHERE NOT (("queries_b"."a_id" IN
                    (SELECT U3."a_id" FROM "queries_d" U3 WHERE
 U3."group_id" IN (1))
                    AND "queries_b"."a_id" IS NOT NULL))

 Which is correct, as the filtering is taken to the a.id field (see the
 inner SELECT).

 Attached is a diff which adds tests showing the query is returning
 expected results.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/18583#comment:2>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to