Opened 5 years ago

Closed 5 years ago

#30456 closed Bug (invalid)

Doing a select_related() after a union() results in an IndexError.

Reported by: Alex Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: invalid
Status: newclosed
Summary: Doing a select_related() after a union() results in an IndexErrorDoing a select_related() after a union() results in an IndexError.
Version: 2.2master

It's unsupported but doesn't raise an error yet. As ​per the documentation, "only LIMIT, OFFSET, COUNT(*), ORDER BY, and specifying columns (i.e. slicing, count(), order_by(), and values()/values_list()) are allowed on the resulting QuerySet.". See #27995 for a ticket to raise a helpful message about this.

Note: See TracTickets for help on using tickets.
Back to Top