summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAttila Tovt <uran198@gmail.com>2015-11-22 02:59:37 +0200
committerTim Graham <timograham@gmail.com>2015-11-25 16:53:23 -0500
commite4bd6923bdc3216fe958f1b1c50e95f4f77e2ada (patch)
treeef0401ad9c2e46a64ed72359d3a4d8f193966f1a
parent5456c85ba8f3c4c0b2f304434414a8a1244df9f1 (diff)
[1.9.x] Fixed #25772 -- Corrected __len lookup on ArrayField for empty arrays.
Backport of 88fc9e2826044110b7b22577a227f122fe9c1fb5 from master
-rw-r--r--django/contrib/postgres/fields/array.py6
-rw-r--r--docs/releases/1.8.8.txt3
-rw-r--r--tests/postgres_tests/test_array.py7
3 files changed, 15 insertions, 1 deletions
diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py
index 4da2a9658b..ff37483b4f 100644
--- a/django/contrib/postgres/fields/array.py
+++ b/django/contrib/postgres/fields/array.py
@@ -202,7 +202,11 @@ class ArrayLenTransform(Transform):
def as_sql(self, compiler, connection):
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):
diff --git a/docs/releases/1.8.8.txt b/docs/releases/1.8.8.txt
index 1c4b659782..1a28efe167 100644
--- a/docs/releases/1.8.8.txt
+++ b/docs/releases/1.8.8.txt
@@ -11,3 +11,6 @@ Bugfixes
* Fixed incorrect ``unique_together`` field name generation by ``inspectdb``
(:ticket:`25274`).
+
+* Corrected ``__len`` query lookup on ``ArrayField`` for empty arrays
+ (:ticket:`25772`).
diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py
index f5047755a7..0ff7198fc2 100644
--- a/tests/postgres_tests/test_array.py
+++ b/tests/postgres_tests/test_array.py
@@ -231,6 +231,13 @@ class TestQuerying(PostgreSQLTestCase):
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):
self.assertSequenceEqual(
NullableIntegerArrayModel.objects.filter(field__0_1=[2]),