Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26968 closed Bug (wontfix)

Additional annotate before values call includes non-relevant fields in GROUP BY clause

Reported by: Chris Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords: orm, sql, annotation
Cc: Simon Charette Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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.

Change History (4)

comment:1 by Simon Charette, 8 years ago

Is this a regression from 1.7? If not then I'm afraid you'll have to upgrade to Django 1.9 as only security and data loss bugs are backported to 1.8.

comment:2 by Tim Graham, 8 years ago

Resolution: wontfix
Status: newclosed

Even if it's a regression from 1.7, I'm afraid it's too late to fix these sort of issues in 1.8.

comment:3 by Chris, 8 years ago

This is actually the same issue as #25095, and it is solved by commit 6024fd5. Applying that commit to the 1.8 branch solves the problem for 1.8 as well.

I.e. there is a tiny fix, it is tested, just not applied to 1.8 only 1.9 and up. Cherry-picking commit 6024fd5 onto stable/1.8.x is almost all that is needed.

comment:4 by Chris, 8 years ago

Easy pickings: set
Note: See TracTickets for help on using tickets.
Back to Top