Opened 8 years ago

Closed 8 years ago

#4333 closed (invalid)

Filtering date gt in SQLite returns identical objects

Reported by: Michael Snoyman <michael+django@…> Owned by: adrian
Component: Database layer (models, ORM) Version: master
Severity: Keywords: sqlite date
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I'm putting together a very simple blog app, and one feature I put in was to have a link to the next newer and older entries. Here's what (the relevant parts of) my model looks like:

class Entry(models.Model):
	when = models.DateField(core=True)
	def newer(self):
		set = Entry.objects.filter(blogger=self.blogger).filter(when__gt=self.when)
		try:
			return set[0]
		except IndexError:
			return None

	def older(self):
		set = Entry.objects.filter(blogger=self.blogger).filter(when__lt=self.when)
		try:
			return set[0]
		except IndexError:
			return None

I run my site using MySQL, and things work properly (ie, for the newest entry, newer returns None, etc). On my test environment, I'm using SQLite. Older works properly; however, when viewing the newest entry, newer returns itself.

Change History (3)

comment:1 Changed 8 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Have you tried the automatically-created get_next_by_when and get_previous_by_when methods to see if they show the same behavior?

comment:2 Changed 8 years ago by Michael Snoyman <michael+django@…>

I just tested them, and they work properly on the SQLite database.

comment:3 Changed 8 years ago by Gary Wilson <gary.wilson@…>

  • Resolution set to invalid
  • Status changed from new to closed

Perhaps you are seeing this behavior because you are returning the first item of your set QuerySet, but the QuerySet is not ordered. So you end up getting a random ordering of Entry objects newer than the given Entry object and returning the first item in that QuerySet. This could lead to duplicate Entry objects being returned since it's not ordered by when date.

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