summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2025-10-13 17:22:17 -0400
committerJacob Walls <jacobtylerwalls@gmail.com>2025-10-14 15:50:26 -0400
commit8baee531d40cd6ee8b342dbcf2c7924d20694969 (patch)
treea4d8c8d0aa4180b20e219d9b3e62d5f57f4d5c38 /django
parent94cbd67d9eb9712be5518c3d5b4c17eac2e63629 (diff)
[5.2.x] Fixed #36648, Refs #33772 -- Accounted for composite pks in first()/last() when aggregating.
Backport of 02eed4f37879b2077496f86bb1378a076b981233 from main.
Diffstat (limited to 'django')
-rw-r--r--django/db/models/query.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 1270141619..535d91d767 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -2031,8 +2031,14 @@ class QuerySet(AltersData):
raise TypeError(f"Cannot use {operator_} operator with combined queryset.")
def _check_ordering_first_last_queryset_aggregation(self, method):
- if isinstance(self.query.group_by, tuple) and not any(
- col.output_field is self.model._meta.pk for col in self.query.group_by
+ if (
+ isinstance(self.query.group_by, tuple)
+ # Raise if the pk fields are not in the group_by.
+ and self.model._meta.pk
+ not in {col.output_field for col in self.query.group_by}
+ and set(self.model._meta.pk_fields).difference(
+ {col.target for col in self.query.group_by}
+ )
):
raise TypeError(
f"Cannot use QuerySet.{method}() on an unordered queryset performing "