#29396: Using date__year comparisons together with OuterRef causes an IndexError
-------------------------------------+-------------------------------------
Reporter: Dmitry | Owner: nobody
Shachnev |
Type: Bug | Status: new
Component: Database | Version:
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 |
-------------------------------------+-------------------------------------
With the following models:
{{{
#!python
class ModelWithDate(models.Model):
id = models.AutoField(primary_key=True)
date = models.DateField()
class ModelWithYear(models.Model):
id = models.AutoField(primary_key=True)
year = models.IntegerField()
date_ref = models.ForeignKey(to=ModelWithDate,
on_delete=models.CASCADE)
}}}
the following code:
{{{
#!python
>>> dates = ModelWithDate.objects.filter(date__year__gte=OuterRef("year"))
>>> dates_subq = Subquery(dates.values("id"))
>>> ModelWithYear.objects.filter(date_ref__in=dates_subq)
}}}
causes an exception:
{{{
#!pytb
Traceback (most recent call last):
...
File "/usr/lib/python3/dist-packages/django/db/models/lookups.py", line
90, in process_rhs
sql, params = compiler.compile(value)
File "/usr/lib/python3/dist-packages/django/db/models/sql/compiler.py",
line 391, in compile
sql, params = node.as_sql(self, self.connection)
File "/usr/lib/python3/dist-packages/django/db/models/expressions.py",
line 1041, in as_sql
template_params['subquery'], sql_params =
self.queryset.query.get_compiler(connection=connection).as_sql()
File "/usr/lib/python3/dist-packages/django/db/models/sql/compiler.py",
line 459, in as_sql
where, w_params = self.compile(self.where) if self.where is not None
else ("", [])
File "/usr/lib/python3/dist-packages/django/db/models/sql/compiler.py",
line 391, in compile
sql, params = node.as_sql(self, self.connection)
File "/usr/lib/python3/dist-packages/django/db/models/sql/where.py",
line 80, in as_sql
sql, params = compiler.compile(child)
File "/usr/lib/python3/dist-packages/django/db/models/sql/compiler.py",
line 391, in compile
sql, params = node.as_sql(self, self.connection)
File "/usr/lib/python3/dist-packages/django/db/models/lookups.py", line
523, in as_sql
start, finish = self.year_lookup_bounds(connection, rhs_params[0])
IndexError: list index out of range
}}}
The same happens if I create an annotation using ExtractYear and use it
directly.
A small test project is attached. The bug can be reproduced with
“DJANGO_SETTINGS_MODULE=settings python3 ./test.py”. Tested with Django
2.0.5 and 1.11.13.
--
Ticket URL: <https://code.djangoproject.com/ticket/29396>
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/050.b20dd41ded6a9d324a8e6075ee510759%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.