[Django] #36419: bulk_update() incorrectly updates top-level JSONField value to JSON 'null' instead of SQL NULL

3 views
Skip to first unread message

Django

unread,
May 26, 2025, 10:41:53 AMMay 26
to django-...@googlegroups.com
#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.

Django

unread,
May 26, 2025, 10:47:32 AMMay 26
to django-...@googlegroups.com
#36419: bulk_update() incorrectly updates top-level JSONField value to JSON 'null'
instead of SQL NULL
-------------------------------------+-------------------------------------
Reporter: Clifford Gama | Owner: Clifford
| Gama
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36419#comment:1>

Django

unread,
May 26, 2025, 4:50:38 PMMay 26
to django-...@googlegroups.com
#36419: bulk_update() incorrectly updates top-level JSONField value to JSON 'null'
instead of SQL NULL
-------------------------------------+-------------------------------------
Reporter: Clifford Gama | Owner: Clifford
| Gama
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* cc: Simon Charette, Sage Abdullah (added)
* stage: Unreviewed => Accepted

Comment:

Hello Clifford, thank you for your investigation on #36418 and for
creating this ticket. If you ask me, I think the reporter in #36418 was
reporting this exact same thing, but I also agree the report was a bit
confusing (since it was not stated what was the expected outcome). I'll be
accepting this ticket based on that, and also because for models with a
`null=False` JSONField, this code does raise `IntegrityError`:
{{{#!python
obj = JSONModel()
obj.value = None
obj.save()
}}}
but `bulk_update` does not.
--
Ticket URL: <https://code.djangoproject.com/ticket/36419#comment:2>
Reply all
Reply to author
Forward
0 new messages