﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37377	JSONIn lookup crashes or returns wrong result for primitives	Jacob Walls	Clifford Gama	"On databases besides Postgres (which has a native `JSONField`), a [https://github.com/django/django/blob/9332b163a67eabe5bdbf8066b1c5da394be9aa9c/django/db/models/fields/json.py#L412-L414 branch] is taken that does not check the `output_field` for a `Value` expression. (Primitive strings, ints, bools, etc., might have other output_fields than `JSONField`.)

{{{#!py
class Person(models.Model):
    data = models.JSONField(null=True)

def run():
    instance = Person.objects.create(data=""primitive"")
    qs = Person.objects.filter(data__in=[models.Value(""primitive"")])
    print(qs)
}}}

This query worked(*) on 6.0, but on 6.1 it returns the wrong result (MariaDB) or crashes (SQLite). Notice for SQLite here, the string ""primitive"" should not be provided to `JSON_EXTRACT()`:

{{{#!sql
SELECT ""app_person"".""id"", ""app_person"".""data"" FROM ""app_person"" WHERE (CASE WHEN JSON_TYPE(""app_person"".""data"", $) IN ('null','false','true') THEN JSON_TYPE(""app_person"".""data"", $) ELSE JSON_EXTRACT(""app_person"".""data"", $) END) IN (JSON_EXTRACT(primitive, '$'))
}}}
{{{#!py
django.db.utils.OperationalError: malformed JSON
}}}


(*) -- On SQLite, you'd have to adjust the second query to use extra quotes, because of some quote weirdness, possibly similar to #32491?"	Bug	assigned	Database layer (models, ORM)	6.1	Release blocker			Clifford Gama	Unreviewed	0	0	0	0	0	0
