#28749: PostgreSQL's ArrayField "in" operator does not allow subqueries
----------------------------------+--------------------------------------
     Reporter:  Michał Pasternak  |                    Owner:  (none)
         Type:  Bug               |                   Status:  new
    Component:  contrib.postgres  |                  Version:  master
     Severity:  Normal            |               Resolution:
     Keywords:                    |             Triage Stage:  Unreviewed
    Has patch:  1                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------
Description changed by Michał Pasternak:

Old description:

> 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.
>
> I am planning to back port this to 1.11.x .
>
> 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.

New description:

 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.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28749#comment:4>
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/068.8209ddf75984228ea9214f1edd3c5cca%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to