1
0
mirror of https://github.com/django/django.git synced 2025-08-12 12:59:14 +00:00

Fixed #33309 -- Fixed QuerySet.distinct() crash on mixed case annotation.

This commit is contained in:
arsalan.ghassemi 2021-11-23 18:22:52 +01:00 committed by Mariusz Felisiak
parent aec71aaa5b
commit bdcda1ca9b
2 changed files with 8 additions and 2 deletions

View File

@ -754,7 +754,7 @@ class SQLCompiler:
targets, alias, _ = self.query.trim_joins(targets, joins, path) targets, alias, _ = self.query.trim_joins(targets, joins, path)
for target in targets: for target in targets:
if name in self.query.annotation_select: if name in self.query.annotation_select:
result.append(name) result.append(self.connection.ops.quote_name(name))
else: else:
r, p = self.compile(transform_function(target, alias)) r, p = self.compile(transform_function(target, alias))
result.append(r) result.append(r)

View File

@ -1,5 +1,5 @@
from django.db import connection from django.db import connection
from django.db.models import CharField, Max from django.db.models import CharField, F, Max
from django.db.models.functions import Lower from django.db.models.functions import Lower
from django.test import TestCase, skipUnlessDBFeature from django.test import TestCase, skipUnlessDBFeature
from django.test.utils import register_lookup from django.test.utils import register_lookup
@ -149,3 +149,9 @@ class DistinctOnTests(TestCase):
""" """
staff = Staff.objects.distinct('name').order_by('name', '-organisation').get(name='p1') staff = Staff.objects.distinct('name').order_by('name', '-organisation').get(name='p1')
self.assertEqual(staff.organisation, 'o2') self.assertEqual(staff.organisation, 'o2')
def test_distinct_on_mixed_case_annotation(self):
qs = Staff.objects.annotate(
nAmEAlIaS=F('name'),
).distinct('nAmEAlIaS').order_by('nAmEAlIaS')
self.assertSequenceEqual(qs, [self.p1_o1, self.p2_o1, self.p3_o1])