#20152: QuerySet filter() and exclude() when used with annotate() generate same 
SQL
----------------------------------------------+--------------------
     Reporter:  raymond.wanyoike@…            |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.5
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 Here are the models:
 {{{
 #!python

 ## Models
 from django.db import models

 class ModelA(models.Model):
     aafield = models.IntegerField()

 class ModelB(models.Model):
     bbfield = models.IntegerField()
     model_a = models.OneToOneField(ModelA)
 }}}

 Here is the query:
 {{{
 #!python

 ## QuerySet
 from django.db.models import Sum, F

 # 1. Get all ModelA objects
 # 2. Annotate sum of modelb__bbfield (OneToOne relationship)
 # 3. Filter where ModelA.aafield == ModelB.bbfield_sum
 result = ModelA.objects.all() \
     .annotate(bbfield_sum=Sum('modelb__bbfield')) \
     .filter(aafield=F('b_field_sum'))
 }}}

 Up to this point the generated SQL is valid and correct:
 {{{
 #!sql

 SELECT ... FROM `...` LEFT OUTER JOIN `...` ON (`...` = `...`) GROUP BY
 `...`
 HAVING `project_modela`.`valid` = SUM(`project_modelb`.`result`)
 }}}

 Here is the problem, if you change {{{filter()}}} to {{{exclude()}}}, the
 same same SQL code is generated. I even tried it using {{{Q()}}} and
 {{{~Q()}}} and got the exact same SQL, the filter cannot be negated. In
 other words .exclude() and ~Q() have no effect.

 I am currently falling back to {{{aafield__gt}}} and {{{aafield__lt}}} to
 have get the effect of == and !=

-- 
Ticket URL: <https://code.djangoproject.com/ticket/20152>
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