﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24530	Index names are inconsistent on PostgreSQL (at least)	Aymeric Augustin	nobody	"1) Create a `test_idx` with the following model:

{{{
from django.db import models

class TestIndex(models.Model):
    foo = models.BooleanField(default=True, db_index=True)
}}}

2) Create initial migrations:

{{{
% ./manage.py makemigrations test_idx
Migrations for 'test_idx':
  0001_initial.py:
    - Create model TestIndex
% ./manage.py sqlmigrate test_idx 0001
BEGIN;
CREATE TABLE ""test_idx_testindex"" (""id"" serial NOT NULL PRIMARY KEY, ""foo"" boolean NOT NULL);
CREATE INDEX ""test_idx_testindex_acbd18db"" ON ""test_idx_testindex"" (""foo"");

COMMIT;
% ./manage.py migrate test_idx
Operations to perform:
  Apply all migrations: test_idx
Running migrations:
  Applying test_idx.0001_initial... OK
}}}

The index is called **test_idx_testindex_acbd18db**.

3) Remove `db_index=True`, create migrations:

{{{
% ./manage.py makemigrations test_idx
Migrations for 'test_idx':
  0002_auto_20150324_1417.py:
    - Alter field foo on testindex
% ./manage.py sqlmigrate test_idx 0002
BEGIN;
DROP INDEX ""test_idx_testindex_acbd18db"";

COMMIT;
% ./manage.py migrate test_idx
Operations to perform:
  Apply all migrations: test_idx
Running migrations:
  Applying test_idx.0002_auto_20150324_1417... OK
}}}

4) Restore `db_index=True`, create migrations:

{{{
% ./manage.py makemigrations test_idx
Migrations for 'test_idx':
  0003_auto_20150324_1417.py:
    - Alter field foo on testindex
% ./manage.py sqlmigrate test_idx 0003
BEGIN;
CREATE INDEX ""test_idx_testindex_foo_685e905b0e683e77_uniq"" ON ""test_idx_testindex"" (""foo"");

COMMIT;
% ./manage.py migrate test_idx 0003
Operations to perform:
  Target specific migration: 0003_auto_20150324_1417, from test_idx
Running migrations:
  Applying test_idx.0003_auto_20150324_1417... OK
}}}

The index is now called **test_idx_testindex_foo_685e905b0e683e77_uniq**.

This is wrong: the index isn't unique.

5) Remove `db_index=True`, create migrations:

{{{
% ./manage.py makemigrations test_idx
Migrations for 'test_idx':
  0004_auto_20150324_1418.py:
    - Alter field foo on testindex
% ./manage.py sqlmigrate test_idx 0004
BEGIN;
DROP INDEX ""test_idx_testindex_foo_9b7cff514ca041_uniq"";

COMMIT;
% ./manage.py migrate test_idx
Operations to perform:
  Apply all migrations: test_idx
Running migrations:
  Applying test_idx.0004_auto_20150324_1418... OK
}}}

Interestingly Django still manages to drop the correct index. It looks at the index names it generated 

----

This may minor, but things can go downhill very quickly if you squash migrations:

- add `db_index=True` to an existing model, this creates an index as in 4)
- squash migrations, the squashed migration will create an index as in 2)
- remove `db_index=True`, Django will try to drop the index as in 3) — because it takes the name from the output of the squashed migration — while it should do it as in 5)

----

Also I'm checking that my production schema actually matches what my migrations generate and that would be easier if index names stayed consistent."	Bug	closed	Migrations	1.7	Normal	duplicate		Markus Holtermann aksheshdoshi@…	Accepted	1	0	1	0	0	0
