diff --git a/AUTHORS b/AUTHORS index 1e295f3b27..381e85f6cf 100644 --- a/AUTHORS +++ b/AUTHORS @@ -431,6 +431,7 @@ answer newbie questions, and generally made Django that much better: james_027@yahoo.com James Aylett James Bennett + James Gillard James Murty James Tauber James Timmins diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py index c247387eb7..eaff032465 100644 --- a/django/contrib/postgres/fields/array.py +++ b/django/contrib/postgres/fields/array.py @@ -297,7 +297,7 @@ class ArrayLenTransform(Transform): return ( "CASE WHEN %(lhs)s IS NULL THEN NULL ELSE " "coalesce(array_length(%(lhs)s, 1), 0) END" - ) % {"lhs": lhs}, params + ) % {"lhs": lhs}, params * 2 @ArrayField.register_lookup diff --git a/docs/releases/4.1.5.txt b/docs/releases/4.1.5.txt index 57d46f204b..40af9eecb1 100644 --- a/docs/releases/4.1.5.txt +++ b/docs/releases/4.1.5.txt @@ -9,4 +9,7 @@ Django 4.1.5 fixes several bugs in 4.1.4. Bugfixes ======== -* ... +* Fixed a long standing bug in the ``__len`` lookup for ``ArrayField`` that + caused a crash of model validation on + :attr:`Meta.constraints ` + (:ticket:`34205`). diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py index 72e3e66895..9aa5bfdbb8 100644 --- a/tests/postgres_tests/test_constraints.py +++ b/tests/postgres_tests/test_constraints.py @@ -76,6 +76,16 @@ class SchemaTests(PostgreSQLTestCase): constraint.validate(IntegerArrayModel, IntegerArrayModel()) constraint.validate(IntegerArrayModel, IntegerArrayModel(field=[1])) + def test_check_constraint_array_length(self): + constraint = CheckConstraint( + check=Q(field__len=1), + name="array_length", + ) + msg = f"Constraint “{constraint.name}” is violated." + with self.assertRaisesMessage(ValidationError, msg): + constraint.validate(IntegerArrayModel, IntegerArrayModel()) + constraint.validate(IntegerArrayModel, IntegerArrayModel(field=[1])) + def test_check_constraint_daterange_contains(self): constraint_name = "dates_contains" self.assertNotIn(