﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28749	"PostgreSQL's ArrayField ""in"" operator does not allow subqueries"	Michał Pasternak	Tim Graham <timograham@…>	"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. "	Bug	closed	contrib.postgres	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
