#10425: Bad SQL generated for
Book.objects.values('author').annotate(Count('author')).count()
-----------------------------------+----------------------------------------
 Reporter:  kmassey                |       Owner:            
   Status:  new                    |   Milestone:            
Component:  ORM aggregation        |     Version:  SVN       
 Keywords:  values annotate count  |       Stage:  Unreviewed
Has_patch:  0                      |  
-----------------------------------+----------------------------------------
 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
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/10425>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to