Opened 9 years ago

Last modified 30 hours ago

#28872 assigned Bug

JSONField __in operator breaks when given a values_list()

Reported by: Jerome Leclanche Owned by: Pycon Korea Sprints
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: VCAMP Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
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 (13)

comment:1 by Jerome Leclanche, 9 years ago

Description: modified (diff)

comment:2 by Tim Graham, 9 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted

comment:3 by VCAMP, 8 years ago

Owner: set to VCAMP
Status: newassigned

comment:4 by VCAMP, 8 years ago

Cc: VCAMP added
Has patch: set
Last edited 8 years ago by VCAMP (previous) (diff)

comment:5 by Tim Graham, 8 years ago

Patch needs improvement: set

comment:6 by Mariusz Felisiak, 6 years ago

Version: 1.11master

With the new implementation of JSONField this issue is PostgreSQL-specific.

comment:7 by Mariusz Felisiak, 6 years ago

Component: contrib.postgresDatabase layer (models, ORM)

comment:8 by Mariusz Felisiak, 3 years ago

Owner: VCAMP removed
Status: assignednew

comment:9 by JaeHyuckSa, 8 months ago

Owner: set to JaeHyuckSa
Patch needs improvement: unset
Status: newassigned

comment:10 by Simon Charette, 8 months ago

Patch needs improvement: set

Patch had a few issues notably

  • It mutated a source expression during compilation
  • It altered sql.Query.select and .annotations in a way that breaks referenceing (group_by, order_by)

I suggested an alternative approach using a subquery pushdown instead.

comment:11 by JaeHyuckSa, 8 months ago

Patch needs improvement: unset

comment:12 by Jacob Walls, 7 weeks ago

Needs tests: set

comment:13 by Antoliny, 30 hours ago

Owner: changed from JaeHyuckSa to Pycon Korea Sprints

Attempting to assign to Pycon Korea Sprints to make it easier to reserve tickets. Pycon Korea ends August 17, this is available for someone else to pick up on the 18th.

Note: See TracTickets for help on using tickets.
Back to Top