﻿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
24972	Removing unique_together constraint makes migration fail on Django 1.8.2 with MySQL	Adam Brenecki	Adam Brenecki	"Migrations containing `migrations.AlterUniqueTogether(name='xxx', unique_together=set([]),)` may fail on Django 1.8 but not on Django 1.7 if using the backend mysql, but in a slightly different way than in #24757.

Given the following models:

{{{#!python
from django.db import models

# Create your models here.
class FkTarget(models.Model):
    pass

class TheModel(models.Model):
    fk = models.ForeignKey(FkTarget)
    other = models.IntegerField()
}}}

And the following migrations:

{{{#!python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='FkTarget',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ],
        ),
        migrations.CreateModel(
            name='TheModel',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('other', models.IntegerField()),
            ],
        ),
        migrations.AddField(
            model_name='themodel',
            name='fk',
            field=models.ForeignKey(default=1, to='ta.FkTarget'),
            preserve_default=False,
        ),
        migrations.AlterUniqueTogether(
            name='themodel',
            unique_together=set([('fk', 'other')]),
        ),
    ]  
}}}

{{{#!python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('ta', '0001_initial'),
    ]

    operations = [
        migrations.AlterUniqueTogether(
            name='themodel',
            unique_together=set([]),
        ),
    ]
}}}

I get the following error when migrating:

{{{
  Applying ta.0001_initial... OK
  Applying ta.0002_auto_20150612_0354...Traceback (most recent call last):
  
    [snip traceback]
    
django.db.utils.OperationalError: (1061, ""Duplicate key name 'ta_themodel_4195ef88'"")
}}}

What seems to be happening here is that the `AddField` is doing a `CREATE INDEX` on the `fk` field, then the final `AlterUniqueTogether` is trying to recreate the already-existing index.

(Interestingly, if the foreign key is introduced as part of the `CreateModel` rather than as an `AddField` - which is how it was in #24757 - I don't see the first `CREATE INDEX` in `sqlmigrate`, and the whole thing works without errors.)

I think I've managed to figure out what's causing it and fix the issue; I'll post my patch up to GitHub shortly.

In case it's relevant:

{{{
$ mysqld --version
150612 13:42:17 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
mysqld  Ver 5.5.43-0ubuntu0.14.04.1-log for debian-linux-gnu on x86_64 ((Ubuntu))
}}}"	Bug	closed	Migrations	1.9	Release blocker	fixed			Ready for checkin	1	0	0	0	0	0
