Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28749 closed Bug (fixed)

PostgreSQL's ArrayField "in" operator does not allow subqueries

Reported by: Michał Pasternak Owned by: Tim Graham <timograham@…>
Component: contrib.postgres Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Michał Pasternak)

Hi,

I have a legitimate use-case, where I want to filter using "in" operator a PostgreSQL ArrayField (contrib.postgres.fields.array.ArrayField) when using a subquery.

Current implementation does not allow that.

You will get a traceback similar to the one below:

Traceback (most recent call last):
  File "/Users/mpasternak/Programowanie/django/tests/postgres_tests/test_array.py", line 185, in test_in_subquery
    "field", flat=True)
  File "/Users/mpasternak/Programowanie/django/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/mpasternak/Programowanie/django/django/db/models/query.py", line 835, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Users/mpasternak/Programowanie/django/django/db/models/query.py", line 853, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Users/mpasternak/Programowanie/django/django/db/models/sql/query.py", line 1252, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/Users/mpasternak/Programowanie/django/django/db/models/sql/query.py", line 1276, in _add_q
    split_subq=split_subq,
  File "/Users/mpasternak/Programowanie/django/django/db/models/sql/query.py", line 1214, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/Users/mpasternak/Programowanie/django/django/db/models/sql/query.py", line 1084, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "/Users/mpasternak/Programowanie/django/django/db/models/lookups.py", line 18, in __init__
    self.rhs = self.get_prep_lookup()
  File "/Users/mpasternak/Programowanie/django/django/contrib/postgres/fields/array.py", line 252, in get_prep_lookup
    for value in values:
TypeError: 'Query' object is not iterable

The fix seems trivial.

I implemented both the fix and the test for it in my branch here: https://github.com/mpasternak/django .

I am submitting a pull request on GitHub in a moment (https://github.com/django/django/pull/9300).

I am planning to back port this to 1.11.x (https://github.com/django/django/pull/9301).

Not sure if this is a Bug or a Feature, classifying as Bug for now.

This is my first contribution to Django Project that, I believe, is going to be merged. I have sent CLA as requested.

Change History (7)

comment:1 by Michał Pasternak, 7 years ago

comment:2 by Michał Pasternak, 7 years ago

Backport to 1.11.x is here: https://github.com/django/django/pull/9301

comment:3 by Michał Pasternak, 7 years ago

Description: modified (diff)

comment:4 by Michał Pasternak, 7 years ago

Description: modified (diff)

comment:5 by Tim Graham, 7 years ago

Triage Stage: UnreviewedReady for checkin

comment:6 by Tim Graham <timograham@…>, 7 years ago

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

In e554b72:

Fixed #28749 -- Added subquery support for ArrayField's in lookup.

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

In e80a014f:

[2.0.x] Fixed #28749 -- Added subquery support for ArrayField's in lookup.

Backport of e554b72a2a5473f3f1e57a32565758757a9e8e87 from master

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