#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.

-- 
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/051.aae6e573ba9d890715593d026590ca15%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to