mirror of
https://github.com/django/django.git
synced 2025-10-27 15:46:10 +00:00
[4.2.x] Fixed #34754 -- Fixed JSONField check constraints validation on NULL values.
The __isnull lookup of JSONField must special case Value(None, JSONField()) left-hand-side in order to be coherent with its convoluted null handling. Since psycopg>=3 offers no way to pass a NULL::jsonb the issue is resolved by optimizing IsNull(Value(None), True | False) to True | False. Regression in5c23d9f0c3. Thanks Alexandre Collet for the report. Backport of3434dbd39dfrom main
This commit is contained in:
committed by
Mariusz Felisiak
parent
951dcbb2e6
commit
3a1863319c
@@ -11,6 +11,7 @@ from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBF
|
||||
from .models import (
|
||||
ChildModel,
|
||||
ChildUniqueConstraintProduct,
|
||||
JSONFieldModel,
|
||||
Product,
|
||||
UniqueConstraintConditionProduct,
|
||||
UniqueConstraintDeferrable,
|
||||
@@ -251,6 +252,25 @@ class CheckConstraintTests(TestCase):
|
||||
)
|
||||
constraint.validate(Product, Product())
|
||||
|
||||
@skipUnlessDBFeature("supports_json_field")
|
||||
def test_validate_nullable_jsonfield(self):
|
||||
is_null_constraint = models.CheckConstraint(
|
||||
check=models.Q(data__isnull=True),
|
||||
name="nullable_data",
|
||||
)
|
||||
is_not_null_constraint = models.CheckConstraint(
|
||||
check=models.Q(data__isnull=False),
|
||||
name="nullable_data",
|
||||
)
|
||||
is_null_constraint.validate(JSONFieldModel, JSONFieldModel(data=None))
|
||||
msg = f"Constraint “{is_null_constraint.name}” is violated."
|
||||
with self.assertRaisesMessage(ValidationError, msg):
|
||||
is_null_constraint.validate(JSONFieldModel, JSONFieldModel(data={}))
|
||||
msg = f"Constraint “{is_not_null_constraint.name}” is violated."
|
||||
with self.assertRaisesMessage(ValidationError, msg):
|
||||
is_not_null_constraint.validate(JSONFieldModel, JSONFieldModel(data=None))
|
||||
is_not_null_constraint.validate(JSONFieldModel, JSONFieldModel(data={}))
|
||||
|
||||
|
||||
class UniqueConstraintTests(TestCase):
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user