#28872: JSONField __in operator breaks when given a values_list()
----------------------------------+--------------------------------------
     Reporter:  Jerome Leclanche  |                    Owner:  (none)
         Type:  Bug               |                   Status:  new
    Component:  contrib.postgres  |                  Version:  1.11
     Severity:  Normal            |               Resolution:
     Keywords:                    |             Triage Stage:  Unreviewed
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------
Description changed by Jerome Leclanche:

Old description:

> Prerequisites
>
> 1. Create a model MyModel1 with the field `resource = JSONField()`.
> 2. Create another model MyModel2 with a field `id =
> CharField(max_length=255)`
>
> Reproduce by doing
> MyModel1.objects.filter(resource__foo__id__in=MyModel2.objects.all().values_list("id"))
>
> Expected result: Should look at MyModel1 for objects with `resource`
> matching `{"foo": {"id": <id>}}`.
>
> Actual result:
>
>     ERROR:  operator does not exist: jsonb = character varying
>     LINE 1: ...AND ("resource" #> '{foo,id}') IN (SELECT...
>
> The actual issue is that when passing a values_list() to __in, the values
> list is not cast to jsonb (unlike when passing a text type). In the
> resulting sql, we can see `IN (SELECT U0."id" AS Col1 FROM mymodel2 U0
> ...)`. Wrapping `to_json(U0."id")` solves the issue.

New description:

 Prerequisites

 1. Create a model MyModel1 with the field `resource = JSONField()`.
 2. Create another model MyModel2 with a field `id =
 CharField(max_length=255)`

 Reproduce by doing
 
`MyModel1.objects.filter(resource__foo__id__in=MyModel2.objects.all().values_list("id"))`

 Expected result: Should look at MyModel1 for objects with `resource`
 matching `{"foo": {"id": <id>}}`.

 Actual result:

     ERROR:  operator does not exist: jsonb = character varying
     LINE 1: ...AND ("resource" #> '{foo,id}') IN (SELECT...

 The actual issue is that when passing a values_list() to __in, the values
 list is not cast to jsonb (unlike when passing a text type). In the
 resulting sql, we can see `IN (SELECT U0."id" AS Col1 FROM mymodel2 U0
 ...)`. Wrapping `to_json(U0."id")` solves the issue.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28872#comment:1>
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.34ba53b77d76b23ed8add9dd6be3edde%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to