﻿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
10425	Bad SQL generated for Book.objects.values('author').annotate(Count('author')).count()	kmassey		"Observed with SVN revision 9984:

Model:
{{{
class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200)
}}}

When using values() with annotate(), the count() method generates bad SQL:
{{{
$ python manage.py shell
Python 2.6.1 (r261:67515, Dec  6 2008, 16:42:21) 
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type ""help"", ""copyright"", ""credits"" or ""license"" for more information.
(InteractiveConsole)
>>> from example.models import *
>>> from django.db.models import Count
>>> qs = Book.objects.values('author').annotate(Count('author'))
>>> qs.count()
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/Users/kevin/django/bug/annotate_count/django/db/models/query.py"", line 329, in count
    return self.query.get_count()
  File ""/Users/kevin/django/bug/annotate_count/django/db/models/sql/query.py"", line 345, in get_count
    number = obj.get_aggregation()[None]
  File ""/Users/kevin/django/bug/annotate_count/django/db/models/sql/query.py"", line 317, in get_aggregation
    result = query.execute_sql(SINGLE)
  File ""/Users/kevin/django/bug/annotate_count/django/db/models/sql/query.py"", line 2097, in execute_sql
    cursor.execute(sql, params)
  File ""/Users/kevin/django/bug/annotate_count/django/db/backends/util.py"", line 19, in execute
    return self.cursor.execute(sql, params)
  File ""/Users/kevin/django/bug/annotate_count/django/db/backends/sqlite3/base.py"", line 190, in execute
    return Database.Cursor.execute(self, query, params)
OperationalError: near ""FROM"": syntax error


}}}

The SQL that gets generated is malformed:
{{{
SELECT  FROM (SELECT ""example_book"".""author"" AS author, COUNT(""example_book"".""author"") FROM ""example_book"" GROUP BY ""example_book"".""author"") subquery
}}}"		closed	Database layer (models, ORM)	dev		fixed	values annotate count		Accepted	0	0	0	0	0	0
