#11096: .values including related fields does not work when specified after an
aggregate's .annotate
--------------------------------------+-------------------------------------
          Reporter:  dgouldin         |         Owner:     
            Status:  new              |     Milestone:     
         Component:  ORM aggregation  |       Version:  SVN
        Resolution:                   |      Keywords:     
             Stage:  Accepted         |     Has_patch:  0  
        Needs_docs:  0                |   Needs_tests:  0  
Needs_better_patch:  0                |  
--------------------------------------+-------------------------------------
Changes (by cmutel):

 * cc: cmu...@gmail.com (added)
  * version:  1.0 => SVN

Comment:

 This doesn't appear to affect sqlite, but is still a problem for
 postgresql using trunk. However, the problem rests with Django's ORM,
 because if ''annotate'' comes after ''values'' then the SQL is correct. I
 assume that postgres is simply stricter in what it accepts. A simple test
 query, using the models in the ticket:

 {{{
 Book.objects.annotate(num_authors=Count('author')).values('author__name')
 }}}

 produces the following SQL:

 {{{
 SELECT "tester_author"."name"
 FROM "tester_book"
 INNER JOIN "tester_author" ON ("tester_book"."author_id" =
 "tester_author"."id")
 GROUP BY "tester_book"."id",
          "tester_book"."name",
          "tester_book"."author_id"
 }}}

 And raises

 {{{
 ProgrammingError: column "tester_author.name" must appear in the GROUP BY
 clause or be used in an aggregate function
 }}}

 However, putting the annotate clause at the end results in the correct
 ""GROUP BY"" clause:

 {{{
 SELECT "tester_author"."name", COUNT("tester_book"."author_id") AS
 "num_authors"
 FROM "tester_book"
 INNER JOIN "tester_author" ON ("tester_book"."author_id" =
 "tester_author"."id")
 GROUP BY "tester_author"."name"
 }}}

 I think this problem is related to tickets
 [http://code.djangoproject.com/ticket/10060 10060] and
 [http://code.djangoproject.com/ticket/12608 12608].

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11096#comment:3>
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 django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to