#36419: bulk_update() incorrectly updates top-level JSONField value to JSON 'null'
instead of SQL NULL
-------------------------------------+-------------------------------------
Reporter: Clifford | Owner: Clifford Gama
Gama |
Type: Bug | Status: assigned
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Failing test:
{{{#!diff
diff --git a/tests/queries/test_bulk_update.py
b/tests/queries/test_bulk_update.py
index 765fa934ca..2d24a74a60 100644
--- a/tests/queries/test_bulk_update.py
+++ b/tests/queries/test_bulk_update.py
@@ -300,6 +300,16 @@ class BulkUpdateTests(TestCase):
JSONFieldNullable.objects.filter(json_field__has_key="c"),
objs
)
+ @skipUnlessDBFeature("supports_json_field")
+ def test_json_field_none_value(self):
+ """Top level None value must be equivalent to SQL NULL in db."""
+ obj = JSONFieldNullable.objects.create(json_field={"k": "v"})
+ obj.json_field = None
+ JSONFieldNullable.objects.bulk_update([obj],
fields=["json_field"])
+ obj.refresh_from_db()
+ qs = JSONFieldNullable.objects.filter(json_field__isnull=True)
+ self.assertSequenceEqual(qs, [obj])
+
def test_nullable_fk_after_related_save(self):
parent = RelatedObject.objects.create()
child = SingleObject()
}}}
This is inconsistent with `create()`, `update()` and `save()` (see
[
https://github.com/django/django/blob/b373721af0e5c3de0986977ac07e3ad55061ecbe/tests/model_fields/test_jsonfield.py#L227
test] and [
https://docs.djangoproject.com/en/5.2/topics/db/queries
/#storing-and-querying-for-none docs]), and can cause unexpected behaviour
when a JSONField has `null=False`, since `JSON null` does not violate
`null=False`.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36419>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.