Skip to content

Commit af3cfc8

Browse files
jgillardfelixxm
authored andcommitted
[4.1.x] Fixed #34205 -- Fixed Meta.constraints validation crash with ArrayField and __len lookup.
Regression in 88fc9e2 that began manifesting in Django 4.1. Backport of c5ed884 from main.
1 parent 3137174 commit af3cfc8

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ answer newbie questions, and generally made Django that much better:
424424
425425
James Aylett
426426
James Bennett <[email protected]>
427+
James Gillard <[email protected]>
427428
James Murty
428429
James Tauber <[email protected]>
429430
James Timmins <[email protected]>

django/contrib/postgres/fields/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def as_sql(self, compiler, connection):
265265
return (
266266
"CASE WHEN %(lhs)s IS NULL THEN NULL ELSE "
267267
"coalesce(array_length(%(lhs)s, 1), 0) END"
268-
) % {"lhs": lhs}, params
268+
) % {"lhs": lhs}, params * 2
269269

270270

271271
@ArrayField.register_lookup

docs/releases/4.1.5.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ Django 4.1.5 fixes several bugs in 4.1.4.
99
Bugfixes
1010
========
1111

12-
* ...
12+
* Fixed a long standing bug in the ``__len`` lookup for ``ArrayField`` that
13+
caused a crash of model validation on
14+
:attr:`Meta.constraints <django.db.models.Options.constraints>`
15+
(:ticket:`34205`).

tests/postgres_tests/test_constraints.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ def test_check_constraint_range_value(self):
6767
RangesModel.objects.create(ints=(20, 50))
6868
RangesModel.objects.create(ints=(10, 30))
6969

70+
def test_check_constraint_array_length(self):
71+
constraint = CheckConstraint(
72+
check=Q(field__len=1),
73+
name="array_length",
74+
)
75+
msg = f"Constraint “{constraint.name}” is violated."
76+
with self.assertRaisesMessage(ValidationError, msg):
77+
constraint.validate(IntegerArrayModel, IntegerArrayModel())
78+
constraint.validate(IntegerArrayModel, IntegerArrayModel(field=[1]))
79+
7080
def test_check_constraint_daterange_contains(self):
7181
constraint_name = "dates_contains"
7282
self.assertNotIn(

0 commit comments

Comments
 (0)