﻿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
30456	Doing a select_related() after a union() results in an IndexError.	Alex	nobody	"**Example code**
{{{
from django.db import models

# Create your models here.

class City(models.Model):
    # ...
    pass

class Person(models.Model):
    # ...
    hometown = models.ForeignKey(
        City,
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
    )

class Book(models.Model):
    # ...
    author = models.ForeignKey(Person, on_delete=models.CASCADE)
}}}

{{{
Person.objects.all().union(Person.objects.all()).select_related('hometown')
}}}


**Traceback:**
{{{
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/lib/python3.6/site-packages/django/db/models/query.py"", line 226, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File ""/lib/python3.6/site-packages/django/db/models/query.py"", line 250, in __iter__
    self._fetch_all()
  File ""/lib/python3.6/site-packages/django/db/models/query.py"", line 1121, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""/lib/python3.6/site-packages/django/db/models/query.py"", line 66, in __iter__
    rel_populator.populate(row, obj)
  File ""/lib/python3.6/site-packages/django/db/models/query.py"", line 1748, in populate
    if obj_data[self.pk_idx] is None:
IndexError: tuple index out of range
}}}

Also, on a different project with some more complicated models in the same Django version the IndexError occurs earlier, I don't know why.

{{{
Traceback (most recent call last):
  File ""lib/python3.6/site-packages/IPython/core/formatters.py"", line 224, in catch_format_error
    r = method(self, *args, **kwargs)
  File ""lib/python3.6/site-packages/IPython/core/formatters.py"", line 702, in __call__
    printer.pretty(obj)
  File ""lib/python3.6/site-packages/IPython/lib/pretty.py"", line 402, in pretty
    return _repr_pprint(obj, self, cycle)
  File ""lib/python3.6/site-packages/IPython/lib/pretty.py"", line 697, in _repr_pprint
    output = repr(obj)
  File ""lib/python3.6/site-packages/django/db/models/query.py"", line 226, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File ""lib/python3.6/site-packages/django/db/models/query.py"", line 250, in __iter__
    self._fetch_all()
  File ""lib/python3.6/site-packages/django/db/models/query.py"", line 1121, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""lib/python3.6/site-packages/django/db/models/query.py"", line 62, in __iter__
    for row in compiler.results_iter(results):
  File ""lib/python3.6/site-packages/django/db/models/sql/compiler.py"", line 847, in results_iter
    row = self.apply_converters(row, converters)
  File ""lib/python3.6/site-packages/django/db/models/sql/compiler.py"", line 830, in apply_converters
    value = row[pos]
IndexError: list index out of range
}}}

Also reproduced it in 1.11
"	Bug	closed	Database layer (models, ORM)	dev	Normal	invalid			Unreviewed	0	0	0	0	0	0
