[Django] #34615: queryset.order_by().first() is not consistent with other queryset behaviours

16 views
Skip to first unread message

Django

unread,
May 31, 2023, 7:53:11 PM5/31/23
to django-...@googlegroups.com
#34615: queryset.order_by().first() is not consistent with other queryset
behaviours
------------------------------------------+--------------------------
Reporter: Iuri de Silvio | Owner: nobody
Type: Uncategorized | Status: assigned
Component: Uncategorized | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+--------------------------
For any other operation, an empty `.order_by()` remove default ordering,
but it orders by `pk` when `.first()` is called.

{{{
>>> User.objects.only("pk").order_by()[:1]
SELECT "auth_user"."id"
FROM "auth_user"
LIMIT 1
}}}

{{{
>>> User.objects.only("pk").order_by().first()
SELECT "auth_user"."id"
FROM "auth_user"
ORDER BY "auth_user"."id" ASC
LIMIT 1
}}}

I think it is an "undefined behavior" and it should build the query
without ordering.

The ~almost~ undocumented implementation for this today should be:

{{{
try:
user = User.objects.only("pk")[0]
except IndexError:
user = None
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34615>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 31, 2023, 7:53:29 PM5/31/23
to django-...@googlegroups.com
#34615: queryset.order_by().first() is not consistent with other queryset
behaviours
-------------------------------------+-------------------------------------

Reporter: Iuri de Silvio | Owner: nobody
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Iuri de Silvio):

* type: Uncategorized => Bug
* component: Uncategorized => Database layer (models, ORM)


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

Django

unread,
May 31, 2023, 8:00:10 PM5/31/23
to django-...@googlegroups.com
#34615: queryset.order_by().first() is not consistent with other queryset
behaviours
-------------------------------------+-------------------------------------
Reporter: Iuri de Silvio | Owner: nobody
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(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
-------------------------------------+-------------------------------------
Changes (by Iuri de Silvio):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:2>

Django

unread,
Jun 1, 2023, 5:08:18 AM6/1/23
to django-...@googlegroups.com
#34615: queryset.order_by().first() is not consistent with other queryset
behaviours
-------------------------------------+-------------------------------------
Reporter: Iuri de Silvio | Owner: nobody
Type: Bug | Status: assigned
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Iuri de Silvio):

* needs_better_patch: 0 => 1


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

Django

unread,
Jun 1, 2023, 5:14:55 AM6/1/23
to django-...@googlegroups.com
#34615: queryset.order_by().first() is not consistent with other queryset
behaviours
-------------------------------------+-------------------------------------
Reporter: Iuri de Silvio | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

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


Comment:

We added `order_by("pk")` only when it's not explicitly ordered, mainly,
to get the consistent and stable behavior on all backends. It's been that
way basically forever and, IMO, we shouldn't change it. You can start a
discussion on DevelopersMailingList if you don't agree.

--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:4>

Django

unread,
Jun 1, 2023, 6:33:10 AM6/1/23
to django-...@googlegroups.com
#34615: queryset.order_by().first() is not consistent with other queryset
behaviours
-------------------------------------+-------------------------------------
Reporter: Iuri de Silvio | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Iuri de Silvio):

Thanks! I disagree a bit because if I'm explicitly disabling ordering with
an empty `order_by`, it is not the same as not defining ordering.

I'll follow up on mailing list.

--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:5>

Django

unread,
Jun 1, 2023, 6:50:28 AM6/1/23
to django-...@googlegroups.com
#34615: queryset.order_by().first() is not consistent with other queryset
behaviours
-------------------------------------+-------------------------------------
Reporter: Iuri de Silvio | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

See related ticket #23805.

--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:6>

Django

unread,
Jun 3, 2023, 4:55:22 AM6/3/23
to django-...@googlegroups.com
#34615: queryset.order_by().first() is not consistent with other queryset
behaviours
-------------------------------------+-------------------------------------
Reporter: Iuri de Silvio | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

> Thanks! I disagree a bit because if I'm explicitly disabling ordering

with an empty order_by, it is not the same as not defining ordering.

I get your original point, but I agree with Mariusz and the original
authors adding this behaviour.

To have a first/last you must have deterministic ordering and not whatever
the database feels like it wants to do otherwise this will start to
introduce subtle soul-destroying bugs. I've numerous tests fail
intermittently (with the same db) because we forgot to add explicit
ordering.

--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:7>

Reply all
Reply to author
Forward
0 new messages