From aab25a69dd09e6717ff86175ff62c29b847a7791 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 17 Feb 2023 20:38:08 -0500 Subject: [4.2.x] Fixed #34346 -- Ordered selected expressions by position. Used the same approach as for #34176 by using selected expressions position to prevent ambiguous aliases in collisions. Thanks henribru for the report. Regression in 04518e310d4552ff7595a34f5a7f93487d78a406. Backport of 278881e37619278789942513916acafaa88d26f3 from main --- tests/ordering/tests.py | 19 +++++++++++++++++++ tests/postgres_tests/test_array.py | 3 +-- 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/ordering/tests.py b/tests/ordering/tests.py index 79e1714ab6..7ff38acc4a 100644 --- a/tests/ordering/tests.py +++ b/tests/ordering/tests.py @@ -8,6 +8,7 @@ from django.db.models import ( DateTimeField, F, Max, + OrderBy, OuterRef, Subquery, Value, @@ -619,3 +620,21 @@ class OrderingTests(TestCase): ), Author.objects.order_by(Length(Upper("name"))), ) + + def test_ordering_select_related_collision(self): + self.assertEqual( + Article.objects.select_related("author") + .annotate(name=Upper("author__name")) + .filter(pk=self.a1.pk) + .order_by(OrderBy(F("name"))) + .first(), + self.a1, + ) + self.assertEqual( + Article.objects.select_related("author") + .annotate(name=Upper("author__name")) + .filter(pk=self.a1.pk) + .order_by("name") + .first(), + self.a1, + ) diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index f58c9477fc..f7615c974e 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -465,10 +465,9 @@ class TestQuerying(PostgreSQLTestCase): {"field__0": 20, "arrayagg": [self.objs[3].pk]}, ], ) - alias = connection.ops.quote_name("field__0") sql = ctx[0]["sql"] self.assertIn("GROUP BY 1", sql) - self.assertIn(f"ORDER BY {alias}", sql) + self.assertIn("ORDER BY 1", sql) def test_index(self): self.assertSequenceEqual( -- cgit v1.3