diff options
| author | Marc Tamlyn <marc.tamlyn@gmail.com> | 2015-01-20 09:52:23 +0000 |
|---|---|---|
| committer | Marc Tamlyn <marc.tamlyn@gmail.com> | 2015-01-20 10:26:32 +0000 |
| commit | c80b2144d22d29725cf4d584bfa9079dd35af064 (patch) | |
| tree | 1dbcbc4ca9d945d1442b10a259245190a10c59a5 | |
| parent | 504cd5d3be450376f462b66e36df6edc923e9496 (diff) | |
[1.8.x] Fixes #24169 -- More arrayfield specific lookups.
varchar()[] cannot compare itself to text[]
Thanks to joelburton for the patch.
Backport of 0ae94d0d31 from master
| -rw-r--r-- | django/contrib/postgres/fields/array.py | 16 | ||||
| -rw-r--r-- | tests/postgres_tests/test_array.py | 12 |
2 files changed, 26 insertions, 2 deletions
diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py index b0850b92e7..af575c6b30 100644 --- a/django/contrib/postgres/fields/array.py +++ b/django/contrib/postgres/fields/array.py @@ -158,8 +158,20 @@ class ArrayContains(lookups.DataContains): return sql, params -ArrayField.register_lookup(lookups.ContainedBy) -ArrayField.register_lookup(lookups.Overlap) +@ArrayField.register_lookup +class ArrayContainedBy(lookups.ContainedBy): + def as_sql(self, qn, connection): + sql, params = super(ArrayContainedBy, self).as_sql(qn, connection) + sql += '::%s' % self.lhs.output_field.db_type(connection) + return sql, params + + +@ArrayField.register_lookup +class ArrayOverlap(lookups.Overlap): + def as_sql(self, qn, connection): + sql, params = super(ArrayOverlap, self).as_sql(qn, connection) + sql += '::%s' % self.lhs.output_field.db_type(connection) + return sql, params @ArrayField.register_lookup diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index 5c300f7ea3..020b31658f 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -156,6 +156,18 @@ class TestQuerying(TestCase): [] ) + def test_contained_by_charfield(self): + self.assertSequenceEqual( + CharArrayModel.objects.filter(field__contained_by=['text']), + [] + ) + + def test_overlap_charfield(self): + self.assertSequenceEqual( + CharArrayModel.objects.filter(field__overlap=['text']), + [] + ) + def test_index(self): self.assertSequenceEqual( NullableIntegerArrayModel.objects.filter(field__0=2), |
