[Django] #30456: Doing a select_related() after a union() results in an IndexError

92 views
Skip to first unread message

Django

unread,
May 7, 2019, 7:17:25 AM5/7/19
to django-...@googlegroups.com
#30456: Doing a select_related() after a union() results in an IndexError
-------------------------------------+-------------------------------------
Reporter: Alex | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
**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

--
Ticket URL: <https://code.djangoproject.com/ticket/30456>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 7, 2019, 7:40:02 AM5/7/19
to django-...@googlegroups.com
#30456: Doing a select_related() after a union() results in an IndexError.

-------------------------------------+-------------------------------------
Reporter: Alex | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* version: 2.2 => master
* resolution: => invalid


Comment:

It's unsupported but doesn't raise an error yet. As
​[https://docs.djangoproject.com/en/stable/ref/models/querysets/#union 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30456#comment:1>

Reply all
Reply to author
Forward
0 new messages