﻿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
20782	Aggregating annotations raises DatabaseError	Debanshu Kundu	nobody	"If I have a model Book defined as:

{{{
class Book(models.Model):
   name = models.CharField(max_length=300)
   pages = models.IntegerField()
   price = models.DecimalField(max_digits=10, decimal_places=2)
   rating = models.FloatField()
   pubdate = models.DateField()

}}}

and I run the query:

{{{
Book.objects.values('rating').annotate(books_per_rating=Count('id')).aggregate(Max('books_per_rating'))
}}}

I get a '''DatabaseError'''.

According to this [https://docs.djangoproject.com/en/1.5/topics/db/aggregation/#aggregating-annotations], Django supports aggregating annotations. But in the example given in the link itself, they annotate over a '''QuerySet''' which in turn returns a '''QuerySet''' (and not '''ValuesQuerySet'''), so aggregate method runs successfully. But in my example aggregating a '''ValuesQuerySet''' raises a '''DatabaseError'''.

Is it a bug in Django? Because if Django does not support aggregation over '''ValuesQuerySet''' then it should raise an exception at Django level (not '''DatabaseError''').


Also interestingly, if I do:

{{{
Book.objects.values('rating').aggregate(Max('rating'))
}}}

It returns an empty dict without performing any DB query!!


'''Note''': Error was raised on 'postgresql_psycopg2' and 'sqlite3' backends. Haven't been tested on other backends."	Bug	closed	Database layer (models, ORM)	1.5	Normal	fixed		Debanshu Kundu	Unreviewed	0	0	0	0	0	0
