#36418: bulk_update None on JSON field sets the value as "null" string in Postgresql -------------------------------------+------------------------------------- Reporter: Amit Maniar | Owner: (none) Type: Bug | Status: new Component: Database layer | Version: 5.2 (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Comment (by Clifford Gama):
Hello, Amit. Thanks for taking the time to create this ticket. I can't reproduce with: {{{#!diff diff --git a/tests/queries/models.py b/tests/queries/models.py index 9f4cf040b6..420b7fee30 100644 --- a/tests/queries/models.py +++ b/tests/queries/models.py @@ -789,3 +789,10 @@ class JSONFieldNullable(models.Model): class Meta: required_db_features = {"supports_json_field"} + + +class NonNullableJSONModel(models.Model): + json_field = models.JSONField(null=False) + + class Meta: + required_db_features = {"supports_json_field"} diff --git a/tests/queries/test_bulk_update.py b/tests/queries/test_bulk_update.py index 765fa934ca..77f90c5082 100644 --- a/tests/queries/test_bulk_update.py +++ b/tests/queries/test_bulk_update.py @@ -26,6 +26,7 @@ from .models import ( SpecialCategory, Tag, Valid, + NonNullableJSONModel ) @@ -300,6 +301,17 @@ class BulkUpdateTests(TestCase): JSONFieldNullable.objects.filter(json_field__has_key="c"), objs ) + @skipUnlessDBFeature("supports_json_field") + @override_settings(DEBUG=True) + def test_non_nullable_json_field(self): + obj = NonNullableJSONModel.objects.create(json_field={"k": "val"}) + obj.json_field = None + NonNullableJSONModel.objects.bulk_update([obj], ["json_field"]) + self.assertIsNone(obj.json_field) + sql = connection.queries[-1]["sql"].lower() + self.assertIn("'null'::jsonb", sql) + self.assertNotIn("'\"null\"'::jsonb", sql) + def test_nullable_fk_after_related_save(self): parent = RelatedObject.objects.create() child = SingleObject() }}} on PostgreSQL 16.8 and Django's main branch. I get a JSON null instead of a `"null"` string. Which is surprising since JSON null is supposed to be stored through `Value(None, JSONField())` [https://docs.djangoproject.com/en/5.2/topics/db/queries/#storing-and- querying-for-none as documented here]. I'd have expected this to use SQL `NULL` instead, raising `IntegrityError`, like `update()` and `save()`. -- Ticket URL: <https://code.djangoproject.com/ticket/36418#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 django-updates+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/django-updates/010701970c633d33-4a1573e7-7147-4d70-bb49-85b37feecc42-000000%40eu-central-1.amazonses.com.