summaryrefslogtreecommitdiff
path: root/tests/aggregation
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2023-01-06 09:10:16 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-01-09 10:52:51 +0100
commitdd68af62b2b27ece50d434f6a351877212e15c3f (patch)
treeca7b43eebd1198c016d3d50a3ade216614237179 /tests/aggregation
parent016bead6a23989adec5c7ee6948db7ce2fc5e89b (diff)
downloaddjango-dd68af62b2b27ece50d434f6a351877212e15c3f.tar.gz
Fixed #34176 -- Fixed grouping by ambiguous aliases.
Regression in b7b28c7c189615543218e81319473888bc46d831. Refs #31377. Thanks Shai Berger for the report and reviews. test_aggregation_subquery_annotation_values_collision() has been updated as queries that are explicitly grouped by a subquery should always be grouped by it and not its outer columns even if its alias collides with referenced table columns. This was not possible to accomplish at the time 10866a10 landed because we didn't have compiler level handling of colliding aliases.
Diffstat (limited to 'tests/aggregation')
-rw-r--r--tests/aggregation/tests.py18
1 files changed, 9 insertions, 9 deletions
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index 67a57c162c..54e1e6f13a 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -1500,27 +1500,29 @@ class AggregateTestCase(TestCase):
],
)
+ @skipUnlessDBFeature("supports_subqueries_in_group_by")
def test_aggregation_subquery_annotation_values_collision(self):
books_rating_qs = Book.objects.filter(
- publisher=OuterRef("pk"),
- price=Decimal("29.69"),
+ pk=OuterRef("book"),
).values("rating")
publisher_qs = (
Publisher.objects.filter(
book__contact__age__gt=20,
- name=self.p1.name,
)
.annotate(
rating=Subquery(books_rating_qs),
- contacts_count=Count("book__contact"),
)
.values("rating")
- .annotate(total_count=Count("rating"))
+ .annotate(total_count=Count("*"))
+ .order_by("rating")
)
self.assertEqual(
list(publisher_qs),
[
- {"rating": 4.0, "total_count": 2},
+ {"rating": 3.0, "total_count": 1},
+ {"rating": 4.0, "total_count": 3},
+ {"rating": 4.5, "total_count": 1},
+ {"rating": 5.0, "total_count": 1},
],
)
@@ -1642,9 +1644,7 @@ class AggregateTestCase(TestCase):
)
with self.assertNumQueries(1) as ctx:
self.assertSequenceEqual(books_qs, [book])
- # Outerquery SELECT, annotation SELECT, and WHERE SELECT but GROUP BY
- # selected alias, if allowed.
- if connection.features.allows_group_by_refs:
+ if connection.features.allows_group_by_select_index:
self.assertEqual(ctx[0]["sql"].count("SELECT"), 3)
@skipUnlessDBFeature("supports_subqueries_in_group_by")