summaryrefslogtreecommitdiff
path: root/django/db
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2017-06-13 08:16:16 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2017-06-13 08:33:26 +0200
commit44e29ea1e906859e85bb2a46ae5ea9d82bd96f5f (patch)
treebc22ec611fad767850afc27d4dc8c6f7874d891e /django/db
parent927d9b51fee2442280ae975b21b98b5a705c4b17 (diff)
[1.11.x] Fixed #28293 -- Fixed union(), intersection(), and difference() when combining with an EmptyQuerySet.
Thanks Jon Dufresne for the report and Tim Graham for the review. Backport of 82175ead723f8fa3f9271fbd4b24275097029aab from master
Diffstat (limited to 'django/db')
-rw-r--r--django/db/models/query.py13
-rw-r--r--django/db/models/sql/compiler.py2
2 files changed, 14 insertions, 1 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index c9ff437232..8a97e45b88 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -838,12 +838,25 @@ class QuerySet(object):
"union() received an unexpected keyword argument '%s'" %
(unexpected_kwarg,)
)
+ # If the query is an EmptyQuerySet, combine all nonempty querysets.
+ if isinstance(self, EmptyQuerySet):
+ qs = [q for q in other_qs if not isinstance(q, EmptyQuerySet)]
+ return qs[0]._combinator_query('union', *qs[1:], **kwargs) if qs else self
return self._combinator_query('union', *other_qs, **kwargs)
def intersection(self, *other_qs):
+ # If any query is an EmptyQuerySet, return it.
+ if isinstance(self, EmptyQuerySet):
+ return self
+ for other in other_qs:
+ if isinstance(other, EmptyQuerySet):
+ return other
return self._combinator_query('intersection', *other_qs)
def difference(self, *other_qs):
+ # If the query is an EmptyQuerySet, return it.
+ if isinstance(self, EmptyQuerySet):
+ return self
return self._combinator_query('difference', *other_qs)
def select_for_update(self, nowait=False, skip_locked=False):
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 9acb56aa84..d1373fcf95 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -379,7 +379,7 @@ class SQLCompiler(object):
features = self.connection.features
compilers = [
query.get_compiler(self.using, self.connection)
- for query in self.query.combined_queries
+ for query in self.query.combined_queries if not query.is_empty()
]
if not features.supports_slicing_ordering_in_compound:
for query, compiler in zip(self.query.combined_queries, compilers):