1
0
mirror of https://github.com/django/django.git synced 2024-12-22 09:05:43 +00:00

Fixed #34205 -- Fixed Meta.constraints validation crash with ArrayField and __len lookup.

Regression in 88fc9e2826 that began
manifesting in Django 4.1.
This commit is contained in:
James Gillard 2022-12-10 15:46:23 +00:00 committed by Mariusz Felisiak
parent b8738aea14
commit c5ed884eab
4 changed files with 16 additions and 2 deletions

View File

@ -431,6 +431,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>

View File

@ -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

View File

@ -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`).

View File

@ -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(