summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaio Ariede <caio.ariede@gmail.com>2015-08-08 09:44:27 -0300
committerTim Graham <timograham@gmail.com>2015-08-15 10:11:09 -0400
commit29fa1b5800f2b86b9860dac38377e95f7c36063f (patch)
tree35a6b52473d51d396f902f5beccccc70585fb30b
parent632a917c11ed9aba384a64cb7c74c9af3f8adfc0 (diff)
[1.8.x] Fixed #25180 -- Prevented varchar_patterns_ops and text_patterns_ops indexes for ArrayField.
Backport of dad8434d6ff5da10959672726dc9b397296d380b from master
-rw-r--r--django/db/backends/postgresql_psycopg2/schema.py5
-rw-r--r--docs/releases/1.8.4.txt3
-rw-r--r--tests/postgres_tests/array_index_migrations/0001_initial.py26
-rw-r--r--tests/postgres_tests/array_index_migrations/__init__.py0
-rw-r--r--tests/postgres_tests/test_array.py27
5 files changed, 61 insertions, 0 deletions
diff --git a/django/db/backends/postgresql_psycopg2/schema.py b/django/db/backends/postgresql_psycopg2/schema.py
index 85d9517c13..9446bfde43 100644
--- a/django/db/backends/postgresql_psycopg2/schema.py
+++ b/django/db/backends/postgresql_psycopg2/schema.py
@@ -26,6 +26,11 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# a second index that specifies their operator class, which is
# needed when performing correct LIKE queries outside the
# C locale. See #12234.
+ #
+ # The same doesn't apply to array fields such as varchar[size]
+ # and text[size], so skip them.
+ if '[' in db_type:
+ continue
if db_type.startswith('varchar'):
output.append(self._create_index_sql(
model, [field], suffix='_like', sql=self.sql_create_varchar_index))
diff --git a/docs/releases/1.8.4.txt b/docs/releases/1.8.4.txt
index b9d7c12c53..dc4864cbcc 100644
--- a/docs/releases/1.8.4.txt
+++ b/docs/releases/1.8.4.txt
@@ -31,3 +31,6 @@ Bugfixes
* Moved the :ref:`unsaved model instance assignment data loss check
<unsaved-model-instance-check-18>` to ``Model.save()`` to allow easier usage
of in-memory models (:ticket:`25160`).
+
+* Prevented ``varchar_patterns_ops`` and ``text_patterns_ops`` indexes for
+ ``ArrayField`` (:ticket:`25180`).
diff --git a/tests/postgres_tests/array_index_migrations/0001_initial.py b/tests/postgres_tests/array_index_migrations/0001_initial.py
new file mode 100644
index 0000000000..8c7688ad44
--- /dev/null
+++ b/tests/postgres_tests/array_index_migrations/0001_initial.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+import django.contrib.postgres.fields
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='CharTextArrayIndexModel',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+ ('char', django.contrib.postgres.fields.ArrayField(models.CharField(max_length=10), db_index=True, size=100)),
+ ('char2', models.CharField(max_length=11, db_index=True)),
+ ('text', django.contrib.postgres.fields.ArrayField(models.TextField(), db_index=True)),
+ ],
+ options={
+ },
+ bases=(models.Model,),
+ ),
+ ]
diff --git a/tests/postgres_tests/array_index_migrations/__init__.py b/tests/postgres_tests/array_index_migrations/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/postgres_tests/array_index_migrations/__init__.py
diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py
index 0dffbaf0a2..589982342e 100644
--- a/tests/postgres_tests/test_array.py
+++ b/tests/postgres_tests/test_array.py
@@ -295,6 +295,33 @@ class TestMigrations(TransactionTestCase):
with connection.cursor() as cursor:
self.assertNotIn(table_name, connection.introspection.table_names(cursor))
+ @override_settings(MIGRATION_MODULES={
+ "postgres_tests": "postgres_tests.array_index_migrations",
+ })
+ def test_adding_arrayfield_with_index(self):
+ """
+ ArrayField shouldn't have varchar_patterns_ops or text_patterns_ops indexes.
+ """
+ table_name = 'postgres_tests_chartextarrayindexmodel'
+ call_command('migrate', 'postgres_tests', verbosity=0)
+ with connection.cursor() as cursor:
+ like_constraint_field_names = [
+ c.rsplit('_', 2)[0].rsplit('_')[-1]
+ for c in connection.introspection.get_constraints(cursor, table_name)
+ if c.endswith('_like')
+ ]
+ # Only the CharField should have a LIKE index.
+ self.assertEqual(like_constraint_field_names, ['char2'])
+ with connection.cursor() as cursor:
+ indexes = connection.introspection.get_indexes(cursor, table_name)
+ # All fields should have regular indexes.
+ self.assertIn('char', indexes)
+ self.assertIn('char2', indexes)
+ self.assertIn('text', indexes)
+ call_command('migrate', 'postgres_tests', 'zero', verbosity=0)
+ with connection.cursor() as cursor:
+ self.assertNotIn(table_name, connection.introspection.table_names(cursor))
+
class TestSerialization(TestCase):
test_data = '[{"fields": {"field": "[\\"1\\", \\"2\\"]"}, "model": "postgres_tests.integerarraymodel", "pk": null}]'