Fixed #32693 -- Quoted and lowercased generated column aliases.

This commit is contained in:
Hasan Ramezani 2021-04-30 13:37:19 +02:00 committed by Mariusz Felisiak
parent 071cf68630
commit 8de4ca74ba
2 changed files with 16 additions and 2 deletions

View File

@ -554,7 +554,10 @@ class SQLCompiler:
if alias:
s_sql = '%s AS %s' % (s_sql, self.connection.ops.quote_name(alias))
elif with_col_aliases:
s_sql = '%s AS %s' % (s_sql, 'Col%d' % col_idx)
s_sql = '%s AS %s' % (
s_sql,
self.connection.ops.quote_name('col%d' % col_idx),
)
col_idx += 1
params.extend(s_params)
out_cols.append(s_sql)

View File

@ -7,7 +7,7 @@ from threading import Lock
from django.core.exceptions import EmptyResultSet, FieldError
from django.db import DEFAULT_DB_ALIAS, connection
from django.db.models import Count, Exists, F, OuterRef, Q
from django.db.models import Count, Exists, F, Max, OuterRef, Q
from django.db.models.expressions import RawSQL
from django.db.models.sql.constants import LOUTER
from django.db.models.sql.where import NothingNode, WhereNode
@ -1855,6 +1855,17 @@ class Queries6Tests(TestCase):
[self.t5, self.t4],
)
def test_col_alias_quoted(self):
with CaptureQueriesContext(connection) as captured_queries:
self.assertEqual(
Tag.objects.values('parent').annotate(
tag_per_parent=Count('pk'),
).aggregate(Max('tag_per_parent')),
{'tag_per_parent__max': 2},
)
sql = captured_queries[0]['sql']
self.assertIn('AS %s' % connection.ops.quote_name('col1'), sql)
class RawQueriesTests(TestCase):
@classmethod