﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26968	Additional annotate before values call includes non-relevant fields in GROUP BY clause	Chris	nobody	"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."	Bug	closed	Database layer (models, ORM)	1.8	Normal	wontfix	orm, sql, annotation	Simon Charette	Unreviewed	0	0	0	0	1	0
