Fixed #25772 -- Corrected __len lookup on ArrayField for empty arrays.
This commit is contained in:
parent
a3708fda35
commit
88fc9e2826
|
@ -202,7 +202,11 @@ class ArrayLenTransform(Transform):
|
||||||
|
|
||||||
def as_sql(self, compiler, connection):
|
def as_sql(self, compiler, connection):
|
||||||
lhs, params = compiler.compile(self.lhs)
|
lhs, params = compiler.compile(self.lhs)
|
||||||
return 'array_length(%s, 1)' % lhs, params
|
# Distinguish NULL and empty arrays
|
||||||
|
return (
|
||||||
|
'CASE WHEN %(lhs)s IS NULL THEN NULL ELSE '
|
||||||
|
'coalesce(array_length(%(lhs)s, 1), 0) END'
|
||||||
|
) % {'lhs': lhs}, params
|
||||||
|
|
||||||
|
|
||||||
class IndexTransform(Transform):
|
class IndexTransform(Transform):
|
||||||
|
|
|
@ -11,3 +11,6 @@ Bugfixes
|
||||||
|
|
||||||
* Fixed incorrect ``unique_together`` field name generation by ``inspectdb``
|
* Fixed incorrect ``unique_together`` field name generation by ``inspectdb``
|
||||||
(:ticket:`25274`).
|
(:ticket:`25274`).
|
||||||
|
|
||||||
|
* Corrected ``__len`` query lookup on ``ArrayField`` for empty arrays
|
||||||
|
(:ticket:`25772`).
|
||||||
|
|
|
@ -231,6 +231,13 @@ class TestQuerying(PostgreSQLTestCase):
|
||||||
self.objs[0:3]
|
self.objs[0:3]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_len_empty_array(self):
|
||||||
|
obj = NullableIntegerArrayModel.objects.create(field=[])
|
||||||
|
self.assertSequenceEqual(
|
||||||
|
NullableIntegerArrayModel.objects.filter(field__len=0),
|
||||||
|
[obj]
|
||||||
|
)
|
||||||
|
|
||||||
def test_slice(self):
|
def test_slice(self):
|
||||||
self.assertSequenceEqual(
|
self.assertSequenceEqual(
|
||||||
NullableIntegerArrayModel.objects.filter(field__0_1=[2]),
|
NullableIntegerArrayModel.objects.filter(field__0_1=[2]),
|
||||||
|
|
Loading…
Reference in New Issue