diff options
| author | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-10-13 17:22:17 -0400 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-10-14 15:49:48 -0400 |
| commit | 28c95a35fbf1a64bb342f622a90fbaaf46315819 (patch) | |
| tree | bc759f4c1d736ff24be9d19ebc8e3d21a84d7a1a /tests | |
| parent | a01d85d9416ca2aa9b3eca00a2f97a24ff25d303 (diff) | |
[6.0.x] Fixed #36648, Refs #33772 -- Accounted for composite pks in first()/last() when aggregating.
Backport of 02eed4f37879b2077496f86bb1378a076b981233 from main.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/composite_pk/test_aggregate.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/tests/composite_pk/test_aggregate.py b/tests/composite_pk/test_aggregate.py index d852fdce30..8a2067cb90 100644 --- a/tests/composite_pk/test_aggregate.py +++ b/tests/composite_pk/test_aggregate.py @@ -141,3 +141,23 @@ class CompositePKAggregateTests(TestCase): msg = "Max expression does not support composite primary keys." with self.assertRaisesMessage(ValueError, msg): Comment.objects.aggregate(Max("pk")) + + def test_first_from_unordered_queryset_aggregation_pk_selected(self): + self.assertEqual( + Comment.objects.values("pk").annotate(max=Max("id")).first(), + {"pk": (1, 1), "max": 1}, + ) + + def test_first_from_unordered_queryset_aggregation_pk_selected_separately(self): + self.assertEqual( + Comment.objects.values("tenant", "id").annotate(max=Max("id")).first(), + {"tenant": 1, "id": 1, "max": 1}, + ) + + def test_first_from_unordered_queryset_aggregation_pk_incomplete(self): + msg = ( + "Cannot use QuerySet.first() on an unordered queryset performing " + "aggregation. Add an ordering with order_by()." + ) + with self.assertRaisesMessage(TypeError, msg): + Comment.objects.values("tenant").annotate(max=Max("id")).first() |
