Opened 18 years ago

Closed 18 years ago

#4333 closed (invalid)

Filtering date gt in SQLite returns identical objects

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

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 by James Bennett, 18 years ago

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 by Michael Snoyman <michael+django@…>, 18 years ago

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

comment:3 by Gary Wilson <gary.wilson@…>, 18 years ago

Resolution: invalid
Status: newclosed

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