summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2022-01-17 18:01:07 +0100
committerGitHub <noreply@github.com>2022-01-17 18:01:07 +0100
commit30a01441347d5a2146af2944b29778fa0834d4be (patch)
treec5f5e76e05822df1542a76b5337b87d7fbe38db1 /django/db/models/sql
parentf37face331f21cb8af70fc4ec101ec7b6be1f63e (diff)
Fixed #29338 -- Allowed using combined queryset in Subquery.
Thanks Eugene Kovalev for the initial patch, Simon Charette for the review, and Chetan Khanna for help.
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/compiler.py9
-rw-r--r--django/db/models/sql/query.py6
2 files changed, 13 insertions, 2 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 69a2d9298f..928ab40254 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -503,7 +503,10 @@ class SQLCompiler:
part_sql = 'SELECT * FROM ({})'.format(part_sql)
# Add parentheses when combining with compound query if not
# already added for all compound queries.
- elif not features.supports_slicing_ordering_in_compound:
+ elif (
+ self.query.subquery or
+ not features.supports_slicing_ordering_in_compound
+ ):
part_sql = '({})'.format(part_sql)
parts += ((part_sql, part_args),)
except EmptyResultSet:
@@ -517,7 +520,9 @@ class SQLCompiler:
combinator_sql = self.connection.ops.set_operators[combinator]
if all and combinator == 'union':
combinator_sql += ' ALL'
- braces = '({})' if features.supports_slicing_ordering_in_compound else '{}'
+ braces = '{}'
+ if not self.query.subquery and features.supports_slicing_ordering_in_compound:
+ braces = '({})'
sql_parts, args_parts = zip(*((braces.format(sql), args) for sql, args in parts))
result = [' {} '.format(combinator_sql).join(sql_parts)]
params = []
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index b13c7b6893..e3fdea6f3a 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1048,6 +1048,12 @@ class Query(BaseExpression):
clone.bump_prefix(query)
clone.subquery = True
clone.where.resolve_expression(query, *args, **kwargs)
+ # Resolve combined queries.
+ if clone.combinator:
+ clone.combined_queries = tuple([
+ combined_query.resolve_expression(query, *args, **kwargs)
+ for combined_query in clone.combined_queries
+ ])
for key, value in clone.annotations.items():
resolved = value.resolve_expression(query, *args, **kwargs)
if hasattr(resolved, 'external_aliases'):