﻿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
26782	DB Index key the same as Unique constraint key in seperate migrations	Jared Mackey	nobody	"When running a migration that has a db_index and a unique constraint it seems the same key is being used for both causing an error when applying migrations.

Migration 1

{{{
class Migration(migrations.Migration):

    dependencies = [
        ('app', '0028_xxxx'),
    ]

    operations = [
        migrations.AlterField(
            model_name='model',
            name='uuid',
            field=models.CharField(db_index=True, max_length=36, blank=True, null=True, unique=False),
        ),
    ]

}}}

This migration runs the following SQL:

{{{
ALTER TABLE ""table"" ALTER COLUMN ""uuid"" DROP NOT NULL; (params [])
CREATE INDEX ""table_uuid_42f8156744ac727f_uniq"" ON ""table"" (""uuid""); (params [])
CREATE INDEX ""table_uuid_42f8156744ac727f_like"" ON ""table"" (""uuid"" varchar_pattern_ops); (params [])
}}}

Migration 2

{{{
def fill_uuids(apps, schema_editor):
    models = apps.get_model('app', 'model')
    models.objects.all().update(uuid=UUID1Now()) # Using postgres to generate uuid 1's


def reverse_uuids(apps, schema_editor):
    models = apps.get_model('app', 'model')
    models.objects.all().update(uuid='')


class Migration(migrations.Migration):
    dependencies = [
        ('app', '0029_xxxxx'),
    ]

    operations = [
        migrations.RunPython(fill_uuids, reverse_code=reverse_uuids),
        migrations.AlterField(
            model_name='model',
            name='uuid',
            field=models.CharField(db_index=True, max_length=36, blank=True, null=False, unique=True),
        ),
    ]
}}}

Migration 2 generates this SQL:

{{{
(0.146) UPDATE ""table"" SET ""uuid"" = uuid_generate_v1(); args=()
ALTER TABLE ""table"" ALTER COLUMN ""uuid"" SET DEFAULT %s, ALTER COLUMN ""uuid"" SET NOT NULL; (params [u''])
(0.002) ALTER TABLE ""table"" ALTER COLUMN ""uuid"" SET DEFAULT '', ALTER COLUMN ""uuid"" SET NOT NULL; args=[u'']
ALTER TABLE ""table"" ADD CONSTRAINT ""table_uuid_42f8156744ac727f_uniq"" UNIQUE (""uuid""); (params [])
(0.010) ALTER TABLE ""table"" ADD CONSTRAINT ""table_uuid_42f8156744ac727f_uniq"" UNIQUE (""uuid""); args=[]
}}}

That causes this exception `django.db.utils.ProgrammingError: relation ""table_uuid_42f8156744ac727f_uniq"" already exists`

"	Bug	new	Uncategorized	1.8	Normal		db, migration		Unreviewed	0	0	0	0	0	0
