diff options
| author | Laurent Tramoy <laurenttramoy@gmail.com> | 2020-05-26 09:11:11 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-05-26 21:14:45 +0200 |
| commit | 2aac176e86204785f0f2ec4838049d8fed70870e (patch) | |
| tree | 812f2d026f8b528567df9e7579f2e1c9d492875c /django/db/models/sql | |
| parent | 51ad767d0b31fcaa0e4ffefa2938b9184c59928a (diff) | |
Fixed #31614 -- Fixed aliases ordering by OrderBy() expressions of combined queryset.
Diffstat (limited to 'django/db/models/sql')
| -rw-r--r-- | django/db/models/sql/compiler.py | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index a1487c4f95..abbb1e37cb 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -6,7 +6,7 @@ from itertools import chain from django.core.exceptions import EmptyResultSet, FieldError from django.db import DatabaseError, NotSupportedError from django.db.models.constants import LOOKUP_SEP -from django.db.models.expressions import OrderBy, Random, RawSQL, Ref, Value +from django.db.models.expressions import F, OrderBy, Random, RawSQL, Ref, Value from django.db.models.functions import Cast from django.db.models.query_utils import Q, select_related_descend from django.db.models.sql.constants import ( @@ -361,13 +361,16 @@ class SQLCompiler: resolved = expr.resolve_expression(self.query, allow_joins=True, reuse=None) if self.query.combinator: 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 # query; necessary since the columns can't be referenced by the # fully qualified name and the simple column names may collide. for idx, (sel_expr, _, col_alias) in enumerate(self.select): if is_ref and col_alias == src.refs: src = src.source - elif col_alias: + elif col_alias and not ( + isinstance(expr_src, F) and col_alias == expr_src.name + ): continue if src == sel_expr: resolved.set_source_expressions([RawSQL('%d' % (idx + 1), ())]) |
