Opened 5 years ago

Closed 5 years ago

#30942 closed Bug (duplicate)

first() and last() return same object (first in both cases) when the order_by field is the same

Reported by: Tadas Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

from datetime import datetime
from django.db import models

class Foo(models.Model):

val = models.IntegerField()
created_time = models.DateTimeField()

created_time = datetime.now()

foo1, _ = Foo.objects.get_or_create(val=1, defaults=dict(created_time=created_time))
foo2, _ = Foo.objects.get_or_create(val=2, defaults=dict(created_time=created_time))

assert Foo.objects.count() == 2

foos = Foo.objects.all().order_by('created_time')
print(foos.first().val, foos.last().val) # prints -> 1 1

assert foo1.val == foos.first().val
assert foo2.val == foos.last().val # FAILS

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #30901.

Note: See TracTickets for help on using tickets.
Back to Top