summaryrefslogtreecommitdiff
path: root/django/db/models/sql/compiler.py
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/compiler.py
parentf37face331f21cb8af70fc4ec101ec7b6be1f63e (diff)
downloaddjango-30a01441347d5a2146af2944b29778fa0834d4be.tar.gz
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/compiler.py')
-rw-r--r--django/db/models/sql/compiler.py9
1 files changed, 7 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 = []