﻿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
34267	QuerySet.union() crashes with IndexError when getting cached results.	Raphaël Stefanini	Francesco Panico	"I tried 4.2a1 today on my project and I noticed pagination was broken on one view.

After little investigation I manage to reproduce an error:

`Event` is a model with `set` a Foreignkey 
{{{
>> events = Event.objects.all()
>> events_without_set = events.filter(set__isnull=True)
>> one_event_of_each_set = (
        events.filter(set__isnull=False)
               .order_by(""set_id"")
               .distinct(""set_id"")
)
>> my_list = events_without_set.union(one_event_of_each_set)
>> my_list.count()
212
>> my_list[2]
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/venv/lib/python3.11/site-packages/django/db/models/query.py"", line 450, in __getitem__
    return qs._result_cache[0]
           ~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
}}}

With django 4.1.5 `my_list[2]` returns an event.
"	Bug	closed	Database layer (models, ORM)	4.2	Release blocker	fixed		Simon Charette	Ready for checkin	1	0	0	0	0	0
