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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
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>
* stage: Unreviewed => Accepted
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:2>
* owner: (none) => VCAMP
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/28872#comment:3>
* cc: VCAMP (added)
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28872#comment:4>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28872#comment:5>
* version: 1.11 => master
Comment:
With the new implementation of `JSONField` this issue is PostgreSQL-
specific.
--
Ticket URL: <https://code.djangoproject.com/ticket/28872#comment:6>
* component: contrib.postgres => Database layer (models, ORM)
--
Ticket URL: <https://code.djangoproject.com/ticket/28872#comment:7>
* owner: VCAMP => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/28872#comment:8>