﻿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
31880	QuerySet.aggregate() mixes annotated fields names.	Thodoris Sotiropoulos	David Wobrock	"I have the following model (simplified for opening the issue).

{{{#!python
class T(models.Model):
  id = models.AutoField(primary=True)
  foo = models.IntegerField()
}}}

and I perform the following query on MySQL (simplified for opening the issue)

{{{#!python
T.objects.annotate(anon=F('foo')).aggregate(foo=Max(F('anon')), sum=Sum(F('foo')))
}}}

this produces the following SQL query

{{{#!sql
SELECT MAX(`anon`), SUM(`foo`) FROM (SELECT `table`.`foo` AS `anon` FROM `foo`) subquery
}}}

that causes the following exception

{{{
django.db.utils.OperationalError: (1054, ""Unknown column 'foo' in 'field list'"")
}}}

I would expect django to generate

{{{#!sql
SELECT MAX(`anon`), SUM(`anon`) FROM (SELECT `table`.`foo` AS `anon` FROM `table`) subquery
}}}"	Bug	closed	Database layer (models, ORM)	3.1	Normal	fixed		David Wobrock Simon Charette	Ready for checkin	1	0	0	0	0	0
