diff --git a/django/db/models/sql/aggregates.py b/django/db/models/sql/aggregates.py index 8a14bdf2df..207bc0c6c8 100644 --- a/django/db/models/sql/aggregates.py +++ b/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 diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index ec477447f3..c0449e7047 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -337,7 +337,7 @@ class Query(object): # 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 @@ class Query(object): 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 @@ class Query(object): # 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] diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index a62adfe0fc..003bf432c0 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -11,6 +11,7 @@ from django.db.models.sql.expressions import SQLEvaluator from django.db.models.sql.query import Query from django.db.models.sql.where import AND, Constraint + __all__ = ['DeleteQuery', 'UpdateQuery', 'InsertQuery', 'DateQuery', 'AggregateQuery'] diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py index c82fd996fd..813cc4839d 100644 --- a/tests/regressiontests/aggregation_regress/tests.py +++ b/tests/regressiontests/aggregation_regress/tests.py @@ -622,6 +622,24 @@ class AggregationTests(TestCase): 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() + ) + @skipUnlessDBFeature('supports_stddev') def test_stddev(self): self.assertEqual(