mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
[5.2.x] Fixed #36648, Refs #33772 -- Accounted for composite pks in first()/last() when aggregating.
Backport of 02eed4f378 from main.
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user