[Django] #30901: first() and last() return same object (first in both cases) when the order_by field is the same

10 views
Skip to first unread message

Django

unread,
Oct 23, 2019, 6:31:41 AM10/23/19
to django-...@googlegroups.com
#30901: first() and last() return same object (first in both cases) when the
order_by field is the same
-----------------------------------------+------------------------
Reporter: Tadas | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.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 |
-----------------------------------------+------------------------
{{{
from django.db import models
from django.utils import timezone

class TestModel(models.Model):
name = models.IntegerField()
created = models.DateTimeField()


dt = timezone.now()
for i in range(5):
TestModel(
name=str(i),
created=dt
).save()

obj = TestModel.objects.all().order_by('created')

print(obj.first().name)
print(obj.last().name)
}}}

Both print statements above will print 0

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

Django

unread,
Oct 23, 2019, 6:32:11 AM10/23/19
to django-...@googlegroups.com
#30901: first() and last() return same object (first in both cases) when the
order_by field is the same
-------------------------------+--------------------------------------
Reporter: Tadas | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 2.2
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 Tadas):

* cc: Tadas (added)
* type: Uncategorized => Bug


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

Django

unread,
Oct 23, 2019, 6:53:47 AM10/23/19
to django-...@googlegroups.com
#30901: first() and last() return same object (first in both cases) when the
order_by field is the same
-------------------------------+--------------------------------------
Reporter: Tadas | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 2.2
Severity: Normal | Resolution: invalid

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 Simon Charette):

* status: new => closed
* resolution: => invalid


Comment:

Ordering on a non-unique field is undefined.

Since all your `TestModel` share the same `created` both `ORDER BY
created_at LIMIT 1` and `ORDER BY created_at DESC LIMIT 1` can return the
same value. This is just how your database backend decides to return it.

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

Reply all
Reply to author
Forward
0 new messages