﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26338	Getting Queryset item by index with PostgreSQL behaves randomly	Paweł Adamczak	nobody	"I originally 'reported' it on [http://stackoverflow.com/q/35831698/3023841 StackOverflow], but this is probably where it should be.

This code on SQLite works as intended and gets two random items from queryset:
{{{
random.seed()
index1, index2 = random.sample(range(0, qs.count()), 2)

all_objects = qs.all()
object1 = all_objects[index1]
object2 = all_objects[index2]
}}}

but when run on PostgreSQL, getting items from Queryset by index seems to behave randomly.

Here are {{{index1, index2}}} and {{{object1, object2}}} pairs when that code is run 5 times:
{{{
9 12
ID: 2754 ID: 2365
15 11
ID: 1626147 ID: 200811
12 1
ID: 2365 ID: 203112
1 12
ID: 203112 ID: 2365
1 3
ID: 203112 ID: 203112
}}}

The last one is especially important, because what it means is {{{qs.all()[1] == qs.all()[3]}}} which just can't be (all ID's are unique). And this occurred quite regularly.

Converting queryset to list fixed the issue ({{{all_objects = list(qs.all())}}}), so I am guessing it has something to do with Queryset laziness in PostgreSQL implementation. I tried to look it up in the source code but with no luck.
"	Bug	closed	Database layer (models, ORM)	1.9	Normal	needsinfo	postgresql		Unreviewed	0	0	0	0	0	0
