From 3dc9f3ac6960c83cd32058677eb0ddb5a5e5da43 Mon Sep 17 00:00:00 2001 From: Ion Alberdi Date: Sun, 9 Oct 2022 11:01:14 +0200 Subject: Fixed #34080 -- Fixed __exact lookup when nested arrays contain only NULL values. Thanks jerch and David Sanders for reviews. --- django/contrib/postgres/fields/array.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'django/contrib/postgres/fields/array.py') diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py index 3edc72ac94..c247387eb7 100644 --- a/django/contrib/postgres/fields/array.py +++ b/django/contrib/postgres/fields/array.py @@ -237,7 +237,9 @@ class ArrayField(CheckFieldDefaultMixin, Field): class ArrayRHSMixin: def __init__(self, lhs, rhs): - if isinstance(rhs, (tuple, list)): + # Don't wrap arrays that contains only None values, psycopg2 doesn't + # allow this. + if isinstance(rhs, (tuple, list)) and any(self._rhs_not_none_values(rhs)): expressions = [] for value in rhs: if not hasattr(value, "resolve_expression"): @@ -256,6 +258,13 @@ class ArrayRHSMixin: cast_type = self.lhs.output_field.cast_db_type(connection) return "%s::%s" % (rhs, cast_type), rhs_params + def _rhs_not_none_values(self, rhs): + for x in rhs: + if isinstance(x, (list, tuple)): + yield from self._rhs_not_none_values(x) + elif x is not None: + yield True + @ArrayField.register_lookup class ArrayContains(ArrayRHSMixin, lookups.DataContains): -- cgit v1.3