Skip to content

Commit

Permalink
Fixed #31916 -- Fixed combined queryset crash when combining with ord…
Browse files Browse the repository at this point in the history
…ered combined querysets.
  • Loading branch information
hramezani authored Sep 17, 2020
1 parent 981a072 commit a046bca
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/db/models/sql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def get_order_by(self):

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
Expand Down
3 changes: 3 additions & 0 deletions tests/queries/test_qs_combinators.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,15 @@ def test_ordering_subqueries(self):
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):
Expand Down

0 comments on commit a046bca

Please sign in to comment.