Opened 11 years ago

Closed 11 years ago

#22152 closed Bug (duplicate)

ORM filter with F object and timedelta offset fails on second call

Reported by: ghickman Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
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

When filtering an ORM call with a datetime field compared to the datetime field of a related object (using an F object) with a timedelta offset the ORM will return a correct result the first time the queryset is executed then throw errors on subsequent calls.

Digging into this further the F object (not the computed datetime) is passed to the DateModifierNode class on the second call, resulting in the AttributeError. Further calls return the IndexError which leads me to think the F object and offset haven't been evaluated. Below is the Django shell output running on an example project (attached).

>>> books = Book.objects.filter(created_at__lte=F('author__created_at') + timedelta(minutes=5))
>>> books
[]
>>> books
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/query.py", line 71, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 709, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 772, in execute_sql
    sql, params = self.as_sql()
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 93, in as_sql
    where, w_params = self.query.where.as_sql(qn=qn, connection=self.connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/where.py", line 106, in as_sql
    sql, params = self.make_atom(child, qn, connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/where.py", line 201, in make_atom
    extra, params = params.as_sql(qn, connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/expressions.py", line 38, in as_sql
    return self.expression.evaluate(self, qn, connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/expressions.py", line 186, in evaluate
    return evaluator.evaluate_date_modifier_node(self, qn, connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/expressions.py", line 112, in evaluate_date_modifier_node
    if timedelta.days == 0 and timedelta.seconds == 0 and \
AttributeError: 'F' object has no attribute 'days'
>>> books
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/query.py", line 71, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 709, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 772, in execute_sql
    sql, params = self.as_sql()
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 93, in as_sql
    where, w_params = self.query.where.as_sql(qn=qn, connection=self.connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/where.py", line 106, in as_sql
    sql, params = self.make_atom(child, qn, connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/where.py", line 201, in make_atom
    extra, params = params.as_sql(qn, connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/expressions.py", line 38, in as_sql
    return self.expression.evaluate(self, qn, connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/expressions.py", line 186, in evaluate
    return evaluator.evaluate_date_modifier_node(self, qn, connection)
  File "/Users/george/.venvs/dmn-test/lib/python2.7/site-packages/django/db/models/sql/expressions.py", line 109, in evaluate_date_modifier_node
    timedelta = node.children.pop()
IndexError: pop from empty list
>>>

Attachments (1)

datemodifiernodetest.tar.gz (3.8 KB ) - added by ghickman 11 years ago.
Test project

Download all attachments as: .zip

Change History (2)

by ghickman, 11 years ago

Attachment: datemodifiernodetest.tar.gz added

Test project

comment:1 by Anssi Kääriäinen, 11 years ago

Resolution: duplicate
Status: newclosed

Seems to be duplicate of now fixed #21643.

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