[Django] #37047: Crash in Query.orderby_issubset_groupby for descending and random order_by strings

17 views
Skip to first unread message

Django

unread,
Apr 18, 2026, 7:52:38 AMApr 18
to django-...@googlegroups.com
#37047: Crash in Query.orderby_issubset_groupby for descending and random order_by
strings
-------------------------------------+-------------------------------------
Reporter: Anže Pečar | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: dev | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
ticket-26434 caused a crash. It can be reproduced with:

{{{
User.objects.values("is_staff").annotate(latest=Max("date_joined")).order_by("-latest").count()
}}}

You should see the following exception:

{{{
django.core.exceptions.FieldError: Cannot resolve keyword '-latest' into
field. Choices are: activity_logs, date_joined, email, first_name, groups,
id, is_active, is_administrator, is_staff, is_superuser, last_login,
last_name, latest, logentry, module_access, password, user_permissions,
username
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/37047>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 18, 2026, 7:59:10 AMApr 18
to django-...@googlegroups.com
#37047: Crash in Query.orderby_issubset_groupby for descending and random order_by
strings
-------------------------------------+-------------------------------------
Reporter: Anže Pečar | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Anže Pečar:

Old description:

> ticket-26434 caused a crash. It can be reproduced with:
>
> {{{
> User.objects.values("is_staff").annotate(latest=Max("date_joined")).order_by("-latest").count()
> }}}
>
> You should see the following exception:
>
> {{{
> django.core.exceptions.FieldError: Cannot resolve keyword '-latest' into
> field. Choices are: activity_logs, date_joined, email, first_name,
> groups, id, is_active, is_administrator, is_staff, is_superuser,
> last_login, last_name, latest, logentry, module_access, password,
> user_permissions, username
> }}}

New description:

#26434 caused a crash. It can be reproduced with:

{{{
User.objects.values("is_staff").annotate(latest=Max("date_joined")).order_by("-latest").count()
}}}

You should see the following exception:

{{{
django.core.exceptions.FieldError: Cannot resolve keyword '-latest' into
field. Choices are: activity_logs, date_joined, email, first_name, groups,
id, is_active, is_administrator, is_staff, is_superuser, last_login,
last_name, latest, logentry, module_access, password, user_permissions,
username
}}}

--
--
Ticket URL: <https://code.djangoproject.com/ticket/37047#comment:1>

Django

unread,
Apr 19, 2026, 3:24:27 AMApr 19
to django-...@googlegroups.com
#37047: Crash in Query.orderby_issubset_groupby for descending and random order_by
strings
-------------------------------------+-------------------------------------
Reporter: Anže Pečar | Owner: Anže
| Pečar
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* owner: (none) => Anže Pečar
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
* status: new => assigned

Comment:

Reproduced, thanks for testing the main branch!
--
Ticket URL: <https://code.djangoproject.com/ticket/37047#comment:2>

Django

unread,
Apr 19, 2026, 3:38:27 AMApr 19
to django-...@googlegroups.com
#37047: Crash in Query.orderby_issubset_groupby for descending and random order_by
strings
-------------------------------------+-------------------------------------
Reporter: Anže Pečar | Owner: Anže
| Pečar
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/37047#comment:3>

Django

unread,
Apr 19, 2026, 4:03:22 AMApr 19
to django-...@googlegroups.com
#37047: Crash in Query.orderby_issubset_groupby for descending and random order_by
strings
-------------------------------------+-------------------------------------
Reporter: Anže Pečar | Owner: Anže
| Pečar
Type: Bug | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls <jacobtylerwalls@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"a284a49153f005f2a7af087025e5112ba06cbd5f" a284a49]:
{{{#!CommitTicketReference repository=""
revision="a284a49153f005f2a7af087025e5112ba06cbd5f"
Fixed #37047 -- Fixed crash in Query.orderby_issubset_groupby for
descending and random order_by strings.

Run this example:

```python
User.objects.values("is_staff").annotate(latest=Max("date_joined")).order_by("-latest").count()
```

You should see the following exception:

```
django.core.exceptions.FieldError: Cannot resolve keyword '-latest' into
field.
```

Regression in 2ce5cb0f7a4618dfdc5f5c10e53e2e9b9543d298.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/37047#comment:4>

Django

unread,
Apr 23, 2026, 9:56:28 AMApr 23
to django-...@googlegroups.com
#37047: Crash in Query.orderby_issubset_groupby for descending and random order_by
strings
-------------------------------------+-------------------------------------
Reporter: Anže Pečar | Owner: Anže
| Pečar
Type: Bug | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Michal Mládek):

I’d like to thank you for catching these two edge cases. Great work.
--
Ticket URL: <https://code.djangoproject.com/ticket/37047#comment:5>
Reply all
Reply to author
Forward
0 new messages