summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2020-09-17 08:26:06 +0200
committerGitHub <noreply@github.com>2020-09-17 08:26:06 +0200
commita046bcadbee2dc0e6c889f82c08b5a21a32359ad (patch)
treee1c15de11f45244802e039e486b39ebf98320c9a
parent981a072dd4dec586f8fc606712ed9a2ef116eeee (diff)
Fixed #31916 -- Fixed combined queryset crash when combining with ordered combined querysets.
-rw-r--r--django/db/models/sql/compiler.py2
-rw-r--r--tests/queries/test_qs_combinators.py3
2 files changed, 4 insertions, 1 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 208f0ddf73..2fedef62fc 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -359,7 +359,7 @@ class SQLCompiler:
for expr, is_ref in order_by:
resolved = expr.resolve_expression(self.query, allow_joins=True, reuse=None)
- if self.query.combinator:
+ if self.query.combinator and self.select:
src = resolved.get_source_expressions()[0]
expr_src = expr.get_source_expressions()[0]
# Relabel order by columns to raw numbers if this is a combined
diff --git a/tests/queries/test_qs_combinators.py b/tests/queries/test_qs_combinators.py
index d12b27522c..d6a5667fe7 100644
--- a/tests/queries/test_qs_combinators.py
+++ b/tests/queries/test_qs_combinators.py
@@ -237,12 +237,15 @@ class QuerySetSetOperationTests(TestCase):
def test_unsupported_ordering_slicing_raises_db_error(self):
qs1 = Number.objects.all()
qs2 = Number.objects.all()
+ qs3 = Number.objects.all()
msg = 'LIMIT/OFFSET not allowed in subqueries of compound statements'
with self.assertRaisesMessage(DatabaseError, msg):
list(qs1.union(qs2[:10]))
msg = 'ORDER BY not allowed in subqueries of compound statements'
with self.assertRaisesMessage(DatabaseError, msg):
list(qs1.order_by('id').union(qs2))
+ with self.assertRaisesMessage(DatabaseError, msg):
+ list(qs1.union(qs2).order_by('id').union(qs3))
@skipIfDBFeature('supports_select_intersection')
def test_unsupported_intersection_raises_db_error(self):