From adab280cefb15659c39558ac26ea392b0a1e456c Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Fri, 14 Jul 2017 18:11:29 +0200 Subject: Fixed #28399 -- Fixed QuerySet.count() for union(), difference(), and intersection() queries. --- django/db/models/sql/compiler.py | 2 +- django/db/models/sql/query.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'django/db/models/sql') diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 84e240d1f4..d53b9d1c5d 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -410,7 +410,7 @@ class SQLCompiler: continue raise if not parts: - return [], [] + raise EmptyResultSet combinator_sql = self.connection.ops.set_operators[combinator] if all and combinator == 'union': combinator_sql += ' ALL' diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 596dc44860..70ea85a275 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -411,12 +411,12 @@ class Query: # aren't smart enough to remove the existing annotations from the # query, so those would force us to use GROUP BY. # - # If the query has limit or distinct, then those operations must be - # done in a subquery so that we are aggregating on the limit and/or - # distinct results instead of applying the distinct and limit after the - # aggregation. + # If the query has limit or distinct, or uses set operations, then + # those operations must be done in a subquery so that the query + # aggregates on the limit and/or distinct results instead of applying + # the distinct and limit after the aggregation. if (isinstance(self.group_by, tuple) or has_limit or has_existing_annotations or - self.distinct): + self.distinct or self.combinator): from django.db.models.sql.subqueries import AggregateQuery outer_query = AggregateQuery(self.model) inner_query = self.clone() -- cgit v1.3