#35381: Regression on json null value constraints in django 4.2
-------------------------------------+-------------------------------------
Reporter: Olivier Tabone | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):
* cc: David Sanders, Simon Charette, Mariusz Felisiak (added)
* stage: Unreviewed => Accepted
* type: Uncategorized => Bug
Comment:
I think this is related to #34754, and I originally thought that this was
another consequence of what [ticket:34754#comment:4 Simon said there]: ''I
think the fundamental problem here is that:''
{{{#!python
MyModel.objects.create(values=None) # Creates an object with a SQL NULL
MyModel.objects.filter(values=None) # Filters for objects with JSON NULL
}}}
but then I ran the proposed test with `--debug-sql` and noticed that the
check code is casting the value in the database to `::JSONB` independently
of whether the value is `None` or not:
{{{
INSERT INTO "ticket_35381_jsonfieldmodel" ("data")
VALUES (NULL) RETURNING "ticket_35381_jsonfieldmodel"."id";
args=(NONE,);
ALIAS=DEFAULT (0.000)
SELECT 1 AS "_check"
WHERE COALESCE((NOT ('null'::JSONB = 'null'::JSONB)), TRUE);
args=(Int4(1),
Jsonb(NONE),
Jsonb(NONE),
TRUE);
ALIAS=DEFAULT
}}}
So I kept digging and bisected the "bad" commit to
5c23d9f0c32f166c81ecb6f3f01d5077a6084318, and something like this makes
the test pass:
{{{#!diff
diff --git a/django/db/models/fields/json.py
b/django/db/models/fields/json.py
index 1b219e620c..9bd6c4b252 100644
--- a/django/db/models/fields/json.py
+++ b/django/db/models/fields/json.py
@@ -103,7 +103,7 @@ class JSONField(CheckFieldDefaultMixin, Field):
value.output_field, JSONField
):
value = value.value
- elif hasattr(value, "as_sql"):
+ elif value is None or hasattr(value, "as_sql"):
return value
return connection.ops.adapt_json_value(value, self.encoder)
}}}
it makes the test
`model_fields.test_jsonfield.TestSaveLoad.test_json_null_different_from_sql_null`
fails for the check on the query:
{{{
NullableJSONModel.objects.filter(value=Value(None, JSONField())) ==
[json_null]
}}}
So further investigation is needed to come up with an acceptable solution.
--
Ticket URL: <https://code.djangoproject.com/ticket/35381#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018eecd0c7e0-58cf8cdc-5191-4ec4-9c26-408bfbb7b1d7-000000%40eu-central-1.amazonses.com.