#26968: Additional annotate before values call includes non-relevant fields in
GROUP BY clause
----------------------------------------------+----------------------------
     Reporter:  chrkr                         |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.8
     Severity:  Normal                        |   Keywords:  orm, sql,
                                              |  annotation
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+----------------------------
 Calling .annotate(), then .values(), then .annotate() with some
 aggregation unexpectedly adds the original annotation to the GROUP BY
 clause.

 Simple example:

 queryset.annotate(unwanted_item=F('a')).values('b').annotate(count=Count('*'))

 The resulting query has GROUP BY "a", "b" instead of just the expected
 GROUP BY "b".

 The SELECT part does NOT include "a", only "b" and "count".

 This does not seem to be in compliance with the documentation, which only
 states that values() followed by annotate() groups by the selected values,
 while annotate() followed by values() hides the annotation column, but no
 mention that annotate().values().annotate() causes the original annotate
 to end up in the grouping as well.

 This seems related to issue #26045.

 This does NOT happen in Django 1.9, but for various reasons (for one, 1.8
 is the LTS version) we need to use the 1.8 version until the next LTS
 comes out.

--
Ticket URL: <https://code.djangoproject.com/ticket/26968>
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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.36c64bf60e413d75b0a6808a986d4297%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to