From 7adb0c8b6008c30cd1d47ba076da4bdbdb4595fe Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 15 Nov 2022 00:15:25 -0500 Subject: [PATCH] Avoided unnecessary usage of RawSQL. This ensures proper alias quoting. --- django/db/models/sql/compiler.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 170bde1d42..16b0bdad70 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -441,11 +441,11 @@ class SQLCompiler: # Add column used in ORDER BY clause to the selected # columns and to each combined query. order_by_idx = len(self.query.select) + 1 - col_name = f"__orderbycol{order_by_idx}" + col_alias = f"__orderbycol{order_by_idx}" for q in self.query.combined_queries: - q.add_annotation(expr_src, col_name) - self.query.add_select_col(resolved, col_name) - resolved.set_source_expressions([RawSQL(f"{order_by_idx}", ())]) + q.add_annotation(expr_src, col_alias) + self.query.add_select_col(resolved, col_alias) + resolved.set_source_expressions([Ref(col_alias, src)]) 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