Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.2.X] Fixed #12687 -- fixed an issue with aggregates and counts in …

…conjunction with annotations where the QuerySet was provably empty. Backport of [14586].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14587 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 03b111c6cc2a09ddf8eea50d8d430a305799e9be 1 parent 929b8ff
@alex alex authored
View
1  django/db/models/sql/aggregates.py
@@ -8,6 +8,7 @@ class AggregateField(object):
"""
def __init__(self, internal_type):
self.internal_type = internal_type
+
def get_internal_type(self):
return self.internal_type
View
20 django/db/models/sql/query.py
@@ -337,7 +337,7 @@ def get_aggregation(self, using):
# information but retrieves only the first row. Aggregate
# over the subquery instead.
if self.group_by is not None:
- from subqueries import AggregateQuery
+ from django.db.models.sql.subqueries import AggregateQuery
query = AggregateQuery(self.model)
obj = self.clone()
@@ -349,7 +349,13 @@ def get_aggregation(self, using):
query.aggregate_select[alias] = aggregate
del obj.aggregate_select[alias]
- query.add_subquery(obj, using)
+ try:
+ query.add_subquery(obj, using)
+ except EmptyResultSet:
+ return dict(
+ (alias, None)
+ for alias in query.aggregate_select
+ )
else:
query = self
self.select = []
@@ -382,13 +388,19 @@ def get_count(self, using):
# 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.
- from subqueries import AggregateQuery
+ from django.db.models.sql.subqueries import AggregateQuery
subquery = obj
subquery.clear_ordering(True)
subquery.clear_limits()
obj = AggregateQuery(obj.model)
- obj.add_subquery(subquery, using=using)
+ try:
+ obj.add_subquery(subquery, using=using)
+ except EmptyResultSet:
+ # add_subquery evaluates the query, if it's an EmptyResultSet
+ # then there are can be no results, and therefore there the
+ # count is obviously 0
+ return 0
obj.add_count_column()
number = obj.get_aggregation(using=using)[None]
View
1  django/db/models/sql/subqueries.py
@@ -10,6 +10,7 @@
from django.db.models.sql.query import Query
from django.db.models.sql.where import AND, Constraint
+
__all__ = ['DeleteQuery', 'UpdateQuery', 'InsertQuery', 'DateQuery',
'AggregateQuery']
View
18 tests/regressiontests/aggregation_regress/tests.py
@@ -641,6 +641,24 @@ def test_more_more_more(self):
lambda: Book.objects.annotate(mean_age=Avg('authors__age')).annotate(Avg('mean_age'))
)
+ def test_empty_filter_count(self):
+ self.assertEqual(
+ Author.objects.filter(id__in=[]).annotate(Count("friends")).count(),
+ 0
+ )
+
+ def test_empty_filter_aggregate(self):
+ self.assertEqual(
+ Author.objects.filter(id__in=[]).annotate(Count("friends")).aggregate(Count("pk")),
+ {"pk__count": None}
+ )
+
+ def test_annotate_and_join(self):
+ self.assertEqual(
+ Author.objects.annotate(c=Count("friends__name")).exclude(friends__name="Joe").count(),
+ Author.objects.count()
+ )
+
if run_stddev_tests():
def test_stddev(self):
self.assertEqual(

0 comments on commit 03b111c

Please sign in to comment.
Something went wrong with that request. Please try again.