Repository: wagtail/wagtail

Issue:
Migrations failing on Django 1.8.8 and 1.9.1 with PostgreSQL
Weird bug.

```
$ python manage.py migrate
...truncated
  Applying wagtailimages.0004_make_focal_point_key_not_nullable... OK
  Applying wagtailimages.0005_make_filter_spec_unique...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 482, in alter_field
    old_db_params, new_db_params, strict)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/backends/postgresql/schema.py", line 116, in _alter_field
    self.execute(like_index_statement)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/mixxorz/.virtualenvs/dj191-wg13/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "wagtailimages_filter_spec_42ad6e02_like" already exists
```

Happens on both Wagtail 1.2 and 1.3, but only on Django 1.8.8 and 1.9.1. Migrations continue to work on Django 1.8.7 and 1.9.0. In addition, migrations still work with SQLite.

A workaround right now is to downgrade the Django version to 1.8.7 or 1.9.0, migrate, then upgrade Django.

This doesn't look like a wagtail issue given that migrations still work for older versions of Django. Heads up though.



Discussion:
Thanks @mixxorz 
This looks like a Django bug (https://code.djangoproject.com/ticket/26034) but we're currently investigating workarounds.


Resolving patch:
diff --git a/wagtail/wagtailimages/migrations/0005_make_filter_spec_unique.py b/wagtail/wagtailimages/migrations/0005_make_filter_spec_unique.py
--- a/wagtail/wagtailimages/migrations/0005_make_filter_spec_unique.py
+++ b/wagtail/wagtailimages/migrations/0005_make_filter_spec_unique.py
@@ -14,7 +14,7 @@ class Migration(migrations.Migration):
         migrations.AlterField(
             model_name='filter',
             name='spec',
-            field=models.CharField(unique=True, max_length=255, db_index=True),
+            field=models.CharField(unique=True, max_length=255),
             preserve_default=True,
         ),
     ]
diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py
--- a/wagtail/wagtailimages/models.py
+++ b/wagtail/wagtailimages/models.py
@@ -366,7 +366,7 @@ class Filter(models.Model):
     """
 
     # The spec pattern is operation1-var1-var2|operation2-var1
-    spec = models.CharField(max_length=255, db_index=True, unique=True)
+    spec = models.CharField(max_length=255, unique=True)
 
     @cached_property
     def operations(self):


Test patch: