summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Gillard <jamesgillard@live.co.uk>2022-12-10 15:46:23 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-12-10 19:39:00 +0100
commitaf3cfc863095e70f752c6b1875ed5c5dbaac2c4a (patch)
treee7729c75470578ef90fe236c944811056b7c3c24
parent3137174344775fa2358e39cd90e6137f292f8daa (diff)
[4.1.x] Fixed #34205 -- Fixed Meta.constraints validation crash with ArrayField and __len lookup.
Regression in 88fc9e2826044110b7b22577a227f122fe9c1fb5 that began manifesting in Django 4.1. Backport of c5ed884eabf3b2b67581c55bf6c87e721f69157f from main.
-rw-r--r--AUTHORS1
-rw-r--r--django/contrib/postgres/fields/array.py2
-rw-r--r--docs/releases/4.1.5.txt5
-rw-r--r--tests/postgres_tests/test_constraints.py10
4 files changed, 16 insertions, 2 deletions
diff --git a/AUTHORS b/AUTHORS
index e5f2bd6d10..f77693fea5 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -424,6 +424,7 @@ answer newbie questions, and generally made Django that much better:
james_027@yahoo.com
James Aylett
James Bennett <james@b-list.org>
+ James Gillard <jamesgillard@live.co.uk>
James Murty
James Tauber <jtauber@jtauber.com>
James Timmins <jameshtimmins@gmail.com>
diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py
index 7269198674..97c6515d95 100644
--- a/django/contrib/postgres/fields/array.py
+++ b/django/contrib/postgres/fields/array.py
@@ -265,7 +265,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 <django.db.models.Options.constraints>`
+ (:ticket:`34205`).
diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py
index 74c07e4fab..a45aed153d 100644
--- a/tests/postgres_tests/test_constraints.py
+++ b/tests/postgres_tests/test_constraints.py
@@ -67,6 +67,16 @@ class SchemaTests(PostgreSQLTestCase):
RangesModel.objects.create(ints=(20, 50))
RangesModel.objects.create(ints=(10, 30))
+ 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(