Opened 3 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#37403 closed Bug (fixed)

JSONIn crashes at database level when preparing a models.Value(None, models.CharField()) on SQLite

Reported by: Jacob Walls Owned by: Clifford Gama
Component: Database layer (models, ORM) Version: 6.1
Severity: Release blocker Keywords: json, output_field, null
Cc: Clifford Gama Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Value(None, output_field=CharField()) represents SQL NULL and compiles without bound parameters. This query worked on 6.0 on SQLite:

from django.db import models

class Person(models.Model):
    metadata = models.JSONField()

def run():
    qs = Person.objects.filter(metadata__in=[models.Value(None, models.CharField())])
    print(qs)
SELECT "app_person"."id",
       "app_person"."metadata"
FROM "app_person"
WHERE "app_person"."metadata" IN (NULL)
LIMIT 21

But after 9b8e4c6d7d47f7b2e96817ba334e9fd53c10a0df (6.1), it crashes on SQLite with:

  File "/Users/jwalls/django/django/db/models/lookups.py", line 349, in <genexpr>
    self.resolve_expression_parameter(compiler, connection, sql, param)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jwalls/django/django/db/models/fields/json.py", line 444, in resolve_expression_parameter
    and params[0] not in connection.ops.jsonfield_datatype_values
        ~~~~~~^^^
IndexError: list index out of range

And differently on 6.1.2 after #37377, at the database level:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 3, and there are 4 supplied.

Change History (6)

comment:1 by Clifford Gama, 3 weeks ago

Owner: set to Clifford Gama
Status: new → assigned

comment:2 by Clifford Gama, 3 weeks ago

Triage Stage: Unreviewed → Accepted

comment:3 by Clifford Gama, 3 weeks ago

Has patch: set

comment:4 by Jacob Walls, 3 weeks ago

Triage Stage: Accepted → Ready for checkin

comment:5 by Jacob Walls <jacobtylerwalls@…>, 3 weeks ago

Resolution: → fixed
Status: assigned → closed

In 1b4d021b:

Fixed #37403 -- Fixed JSONField in crash on SQLite with Value(None) expressions.

Regression in 9b8e4c6d7d47f7b2e96817ba334e9fd53c10a0df.

comment:6 by Jacob Walls <jacobtylerwalls@…>, 3 weeks ago

In 0931d3c3:

[6.1.x] Fixed #37403 -- Fixed JSONField in crash on SQLite with Value(None) expressions.

Regression in 9b8e4c6d7d47f7b2e96817ba334e9fd53c10a0df.

Backport of 1b4d021b6525bff6439b9d3b8d5388a1a1906ecd from main.

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