On Tue, Aug 25, 2009 at 6:28 PM, peterf81<peter...@gmail.com> wrote: > > Hello, > what could be the problem when from this code : > > reputation = Repute.objects.extra( > select={'positive': 'sum(positive)', 'negative': 'sum > (negative)', 'question_id':'question_id', > 'title': 'question.title'}, > tables=['repute', 'question'], > order_by=['-reputed_at'], > where=['user_id=%s AND question_id=question.id'], > params=[user.id] > ).values('positive', 'negative', 'question_id', 'title', > 'reputed_at', 'reputation') > reputation.query.group_by = ['question_id'] > > this SQL query is generated : > > SELECT (sum(positive)) AS `positive`, (question.title) AS `title`, (sum > (negative)) AS `negative`, (question_id) AS `question_id`, > `repute`.`reputed_at`, `repute`.`reputation` > FROM `repute` , `question` > WHERE user_id=1 AND question_id=question.id > GROUP BY question_id, sum(positive), question.title, sum(negative), > question_id > ORDER BY `repute`.`reputed_at` > DESC LIMIT 21 > > I think there are too much GROUP BY columns. It should be only "GROUP > BY question_id". I get the error "Caught an exception while rendering: > (1111, 'Invalid use of group function')".
There's exactly the right amount of GROUP BY information, given a known limitation of the extra() clause. Django currently assumes that every column in the select argument to extra is a normal column that needs to be included in the group by, since there is no way for Django to tell if an extra selected column is an aggregate or not. You're also modifying the internal query attribute of the queryset, which is highly non-recommended behaviour. Luckily, it appears that what you are trying to do can be achieved using the aggregate() modifier on querysets (provided you are using Django v1.1). See the documentation for more details, but your query will end up being something like: Repute.objects.annotate(positive=Sum('question__positive'),negative=Sum('question__negative')) Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---