Opened 14 months ago

Last modified 3 months ago

#28872 assigned Bug

JSONField __in operator breaks when given a values_list()

Reported by: Jerome Leclanche Owned by: VCAMP
Component: contrib.postgres Version: 1.11
Severity: Normal Keywords:
Cc: VCAMP Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

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.

Change History (5)

comment:1 Changed 14 months ago by Jerome Leclanche

Description: modified (diff)

comment:2 Changed 13 months ago by Tim Graham

Description: modified (diff)
Triage Stage: UnreviewedAccepted

comment:3 Changed 3 months ago by VCAMP

Owner: set to VCAMP
Status: newassigned

comment:4 Changed 3 months ago by VCAMP

Cc: VCAMP added
Has patch: set
Last edited 3 months ago by VCAMP (previous) (diff)

comment:5 Changed 3 months ago by Tim Graham

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top