Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#26071 closed Bug (fixed)

IN queries inside conditional case expressions crashes on django 1.8

Reported by: Alberto Avila Owned by: Tim Graham <timograham@…>
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Alberto Avila)

When making a IN filter expression inside a conditional case expression django crashes with the following exception:

AttributeError: 'SubqueryConstraint' object has no attribute 'lhs'

The stacktrace looks like:

    lib/python2.7/site-packages/django/db/models/query.py line 794 in annotate
      obj.query.add_annotation(annotation, alias, is_summary=False)
    lib/python2.7/site-packages/django/db/models/sql/query.py line 983 in add_annotation
      summarize=is_summary)
    lib/python2.7/site-packages/django/db/models/aggregates.py line 21 in resolve_expression
      if c.source_expressions[0].contains_aggregate and not summarize:
    lib/python2.7/site-packages/django/utils/functional.py line 59 in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    lib/python2.7/site-packages/django/db/models/expressions.py line 179 in contains_aggregate
      if expr and expr.contains_aggregate:
    lib/python2.7/site-packages/django/utils/functional.py line 59 in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    lib/python2.7/site-packages/django/db/models/expressions.py line 179 in contains_aggregate
      if expr and expr.contains_aggregate:
    lib/python2.7/site-packages/django/utils/functional.py line 59 in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    lib/python2.7/site-packages/django/db/models/sql/where.py line 324 in contains_aggregate
      return self._contains_aggregate(self)
    lib/python2.7/site-packages/django/db/models/sql/where.py line 320 in _contains_aggregate
      return any(cls._contains_aggregate(c) for c in obj.children)
    lib/python2.7/site-packages/django/db/models/sql/where.py line 320 in <genexpr>
      return any(cls._contains_aggregate(c) for c in obj.children)
    lib/python2.7/site-packages/django/db/models/sql/where.py line 319 in _contains_aggregate
      return getattr(obj.lhs, 'contains_aggregate', False) or getattr(obj.rhs, 'contains_aggregate', False)
   AttributeError: 'SubqueryConstraint' object has no attribute 'lhs'

This error is already fixed on django 1.9 branch. The problem was fixed with this commit:

https://github.com/django/django/commit/afe0bb7b13bb8dc4370f32225238012c873b0ee3#diff-73bc0dcb1a1c931adcf1d344abca22ffR180

I'll create a PR with the changes from that commit that fixes the issue, not sure if the entire commit can be (or should be) backported to 1.8

UPDATE: PR with only the changes needed and its test: https://github.com/django/django/pull/5967

Change History (8)

comment:1 Changed 5 years ago by Alberto Avila

Description: modified (diff)

comment:2 Changed 5 years ago by Alberto Avila

Description: modified (diff)

comment:3 Changed 5 years ago by Alberto Avila

Description: modified (diff)

comment:4 Changed 5 years ago by Tim Graham

Has patch: set
Owner: changed from nobody to Tim Graham
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:5 Changed 5 years ago by Tim Graham

Owner: Tim Graham deleted
Patch needs improvement: set
Status: assignednew

The new test needs to be fixed on Oracle: ORA-00932: inconsistent datatypes: expected - got BLOB.

comment:6 Changed 5 years ago by Tim Graham <timograham@…>

Owner: set to Tim Graham <timograham@…>
Resolution: fixed
Status: newclosed

In 5b3c66d:

[1.8.x] Fixed #26071 -- Fixed crash with in lookup in a Case expression.

Partial backport of afe0bb7b13bb8dc4370f32225238012c873b0ee3 from master.

comment:7 Changed 5 years ago by Tim Graham <timograham@…>

In 16a52cf:

[1.9.x] Refs #26071 -- Added test for in lookup in a Case expression.

Forwardport of 5b3c66d8b64add684489f5010e43de73e7acdd04 from stable/1.8.x

comment:8 Changed 5 years ago by Tim Graham <timograham@…>

In cca151d1:

Refs #26071 -- Added test for in lookup in a Case expression.

Forwardport of 5b3c66d8b64add684489f5010e43de73e7acdd04 from stable/1.8.x

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