Fixed #30093 -- Fixed ordering of combined queryset ordered by F expressions.
This commit is contained in:
parent
b2c598e30d
commit
b86bb47818
|
@ -339,8 +339,9 @@ class SQLCompiler:
|
|||
seen = set()
|
||||
|
||||
for expr, is_ref in order_by:
|
||||
resolved = expr.resolve_expression(self.query, allow_joins=True, reuse=None)
|
||||
if self.query.combinator:
|
||||
src = expr.get_source_expressions()[0]
|
||||
src = resolved.get_source_expressions()[0]
|
||||
# Relabel order by columns to raw numbers if this is a combined
|
||||
# query; necessary since the columns can't be referenced by the
|
||||
# fully qualified name and the simple column names may collide.
|
||||
|
@ -350,12 +351,10 @@ class SQLCompiler:
|
|||
elif col_alias:
|
||||
continue
|
||||
if src == sel_expr:
|
||||
expr.set_source_expressions([RawSQL('%d' % (idx + 1), ())])
|
||||
resolved.set_source_expressions([RawSQL('%d' % (idx + 1), ())])
|
||||
break
|
||||
else:
|
||||
raise DatabaseError('ORDER BY term does not match any column in the result set.')
|
||||
resolved = expr.resolve_expression(
|
||||
self.query, allow_joins=True, reuse=None)
|
||||
sql, params = self.compile(resolved)
|
||||
# Don't add the same column twice, but the order direction is
|
||||
# not taken into account so we strip it. When this entire method
|
||||
|
|
|
@ -110,6 +110,11 @@ class QuerySetSetOperationTests(TestCase):
|
|||
qs2 = Number.objects.filter(num__gte=2, num__lte=3)
|
||||
self.assertNumbersEqual(qs1.union(qs2).order_by('-num'), [3, 2, 1, 0])
|
||||
|
||||
def test_ordering_by_f_expression(self):
|
||||
qs1 = Number.objects.filter(num__lte=1)
|
||||
qs2 = Number.objects.filter(num__gte=2, num__lte=3)
|
||||
self.assertNumbersEqual(qs1.union(qs2).order_by(F('num').desc()), [3, 2, 1, 0])
|
||||
|
||||
def test_union_with_values(self):
|
||||
ReservedName.objects.create(name='a', order=2)
|
||||
qs1 = ReservedName.objects.all()
|
||||
|
|
Loading…
Reference in New Issue