Opened 3 weeks ago
Last modified 2 weeks ago
#37402 assigned Bug
AttributeError when calling first()/last() on an unordered queryset performing aggregation and grouping by an annotation
| Reported by: | Jacob Walls | Owned by: | Django Sprints |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 5.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
02eed4f37879b2077496f86bb1378a076b981233 (#36648) assumed all members of a GROUP BY were Cols. That introduced an AttributeError for cases involving a function output like Lower, shown here:
from django.db import models from django.db.models.functions import Lower class Person(models.Model): name = models.CharField(max_length=100) def run(): # Should raise informative TypeError: # Cannot use QuerySet.first() on an unordered queryset performing aggregation. # Add an ordering with order_by(). first = ( Person.objects .values("name") .annotate(lower=Lower("name"), count=models.Count("id")) .first() )
File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 1187, in first self._check_ordering_first_last_queryset_aggregation(method="first") File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 2330, in _check_ordering_first_last_queryset_aggregation {col.target for col in self.query.group_by} ^^^^^^^^^^ AttributeError: 'Ref' object has no attribute 'target'
It's introduced in 5.2, but we could consider backporting to 6.1 as a "crashing bug".
Change History (4)
comment:1 by , 3 weeks ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 2 weeks ago
| Owner: | changed from to |
|---|
comment:3 by , 2 weeks ago
| Has patch: | set |
|---|
comment:4 by , 2 weeks ago
| Has patch: | unset |
|---|---|
| Owner: | changed from to |
Please leave the Django Sprints tickets for sprints or other contribution events. Those are reserved for mentorship opportunities within the community. When re-assigning a ticket to yourself that's assigned to someone else, it should be unmodified for the last 6 months. If it's sooner than that, it can be acceptable to comment, politely asking if they are still working on the issue. Do not reassign it to yourself though until they respond.
Pull request created: https://github.com/django/django/pull/22136