Opened 3 months ago

Last modified 2 months ago

#36445 closed Bug

Value(None, output_field=JSONField()) incorrectly saves as SQL NULL in bulk_update() — at Initial Version

Reported by: Clifford Gama Owned by:
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Simon Charette, Thomas Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

When using bulk_update() with a Value(None, output_field=JSONField()) expression, the value is incorrectly saved as a SQL NULL rather than a JSON null. This behavior was temporarily fixed in 00c690efbc0b10f67924687f24a7b30397bf47d9 due to incomplete for_save propagation during expression resolution (bug in #36419), but reappears after the proper restoration of for_save handling in #36419.

  • tests/queries/test_bulk_update.py

    djGHOST@anonymous:~/code/django$ git diff
    diff --git a/tests/queries/test_bulk_update.py b/tests/queries/test_bulk_update.py
    index 480fac6784..6c553421cb 100644
    a b from math import ceil  
    33
    44from django.core.exceptions import FieldDoesNotExist
    55from django.db import connection
    6 from django.db.models import F
     6from django.db.models import F, Value, JSONField
    77from django.db.models.functions import Coalesce, Lower
    88from django.db.utils import IntegrityError
    99from django.test import TestCase, override_settings, skipUnlessDBFeature
    class BulkUpdateTests(TestCase):  
    315315                sql_null_qs = JSONFieldNullable.objects.filter(json_field__isnull=True)
    316316                self.assertSequenceEqual(sql_null_qs, [obj])
    317317
     318    @skipUnlessDBFeature("supports_json_field")
     319    def test_json_field_json_null_value(self):
     320        obj = JSONFieldNullable.objects.create(json_field={})
     321        obj.json_field = Value(None, output_field=JSONField())
     322        JSONFieldNullable.objects.bulk_update([obj], fields=["json_field"])
     323        obj.refresh_from_db()
     324        json_null_qs = JSONFieldNullable.objects.filter(json_field=None)
     325        self.assertSequenceEqual(json_null_qs, [obj])
     326
    318327    def test_nullable_fk_after_related_save(self):
    319328        parent = RelatedObject.objects.create()
    320329        child = SingleObject()

Change History (0)

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