summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2010-11-17 02:58:59 +0000
committerAlex Gaynor <alex.gaynor@gmail.com>2010-11-17 02:58:59 +0000
commit03b111c6cc2a09ddf8eea50d8d430a305799e9be (patch)
tree7eb2e11c8da33b0bc7df0a8fa35c46c7a1c2f2a9 /django/db/models/sql
parent929b8ff3da31e1b143979a32659f741bbdf5d22b (diff)
[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
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/aggregates.py1
-rw-r--r--django/db/models/sql/query.py20
-rw-r--r--django/db/models/sql/subqueries.py1
3 files changed, 18 insertions, 4 deletions
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 a066dfeca8..a0bdc94a2c 100644
--- a/django/db/models/sql/subqueries.py
+++ b/django/db/models/sql/subqueries.py
@@ -10,6 +10,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']