#26338: Getting Queryset item by index with PostgreSQL behaves randomly
----------------------------------------------+------------------------
Reporter: pawelad | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Keywords: postgresql
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/26338>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/050.c1d61585175b6ba4c9966f0be96c1a13%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.