Skip to content

Commit

Permalink
Fixed #10425 -- Corrected the interaction of .count() with .annotate(…
Browse files Browse the repository at this point in the history
…) when .values() is also involved. Thanks to kmassey for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10053 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Mar 14, 2009
1 parent 83c1572 commit 84ce18f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion django/db/models/sql/query.py
Expand Up @@ -329,7 +329,7 @@ def get_count(self):
Performs a COUNT() query using the current filter constraints.
"""
obj = self.clone()
if len(self.select) > 1:
if len(self.select) > 1 or self.aggregate_select:
# If a select clause exists, then the query has already started to
# specify the columns that are to be returned.
# In this case, we need to use a subquery to evaluate the count.
Expand Down Expand Up @@ -1950,6 +1950,7 @@ def add_count_column(self):
# Clear out the select cache to reflect the new unmasked aggregates.
self.aggregates = {None: count}
self.set_aggregate_mask(None)
self.group_by = None

def add_select_related(self, fields):
"""
Expand Down
7 changes: 7 additions & 0 deletions tests/regressiontests/aggregation_regress/models.py
Expand Up @@ -252,6 +252,13 @@ class Clues(models.Model):
>>> [int(x['sheets']) for x in qs]
[150, 175, 224, 264, 473, 566]
# Regression for 10425 - annotations don't get in the way of a count() clause
>>> Book.objects.values('publisher').annotate(Count('publisher')).count()
4
>>> Book.objects.annotate(Count('publisher')).values('publisher').count()
6
"""
}

Expand Down

0 comments on commit 84ce18f

Please sign in to comment.