summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2017-07-10 19:45:09 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2017-07-10 20:40:08 +0200
commitca74e563500e291480f1976b58fcd34aac768dca (patch)
tree4da3677329466e374d114b4e8d7641260db4b71b /django/db/models/sql
parent9bca0d0b38d941fe7f3842cb2259d018823ed25e (diff)
Fixed #28378 -- Fixed union() and difference() when combining with a queryset raising EmptyResultSet.
Thanks Jon Dufresne for the report. Thanks Tim Graham and Simon Charette for the reviews.
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/compiler.py28
1 files changed, 18 insertions, 10 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index c705d33af8..84e240d1f4 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -399,7 +399,18 @@ class SQLCompiler:
raise DatabaseError('LIMIT/OFFSET not allowed in subqueries of compound statements.')
if compiler.get_order_by():
raise DatabaseError('ORDER BY not allowed in subqueries of compound statements.')
- parts = (compiler.as_sql() for compiler in compilers)
+ parts = ()
+ for compiler in compilers:
+ try:
+ parts += (compiler.as_sql(),)
+ except EmptyResultSet:
+ # Omit the empty queryset with UNION and with DIFFERENCE if the
+ # first queryset is nonempty.
+ if combinator == 'union' or (combinator == 'difference' and parts):
+ continue
+ raise
+ if not parts:
+ return [], []
combinator_sql = self.connection.ops.set_operators[combinator]
if all and combinator == 'union':
combinator_sql += ' ALL'
@@ -422,16 +433,7 @@ class SQLCompiler:
refcounts_before = self.query.alias_refcount.copy()
try:
extra_select, order_by, group_by = self.pre_sql_setup()
- distinct_fields = self.get_distinct()
-
- # This must come after 'select', 'ordering', and 'distinct' -- see
- # docstring of get_from_clause() for details.
- from_, f_params = self.get_from_clause()
-
for_update_part = None
- where, w_params = self.compile(self.where) if self.where is not None else ("", [])
- having, h_params = self.compile(self.having) if self.having is not None else ("", [])
-
combinator = self.query.combinator
features = self.connection.features
if combinator:
@@ -439,6 +441,12 @@ class SQLCompiler:
raise NotSupportedError('{} is not supported on this database backend.'.format(combinator))
result, params = self.get_combinator_sql(combinator, self.query.combinator_all)
else:
+ distinct_fields = self.get_distinct()
+ # This must come after 'select', 'ordering', and 'distinct'
+ # (see docstring of get_from_clause() for details).
+ from_, f_params = self.get_from_clause()
+ where, w_params = self.compile(self.where) if self.where is not None else ("", [])
+ having, h_params = self.compile(self.having) if self.having is not None else ("", [])
result = ['SELECT']
params = []