Fixed #28399 -- Fixed QuerySet.count() for union(), difference(), and intersection() queries.

This commit is contained in:
Florian Apolloner 2017-07-14 18:11:29 +02:00 committed by Tim Graham
parent 9290f15bb5
commit adab280cef
5 changed files with 39 additions and 11 deletions

View file

@ -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()