﻿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
27558	Setting db_index=False on existing ForeignKey causes constraint to be recreated on MySQL	Ed Morley	Ed Morley	"Using:
* Django 1.10.3
* MySQL 5.6

STR:
1) Start with this models.py:
{{{#!python
class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)


class Album(models.Model):
    artist = models.ForeignKey(Musician)
    name = models.CharField(max_length=100)

    class Meta:
        unique_together = ('artist', 'name')
}}}

2) Run `./manage.py makemigrations && ./manage.py migrate` to generate and apply the initial migration.
3) Add `db_index=False` to the `ForeignKey` (to work around the duplicate index issue discussed [https://groups.google.com/forum/#!topic/django-developers/3ywugkcaxqs here]) - making it:
{{{#!python
    artist = models.ForeignKey(Musician, db_index=False)
}}}
4) Run `./manage.py makemigrations`
5) Run `./manage.py sqlmigrate <foo> 0002` to display the generated SQL

Expected:
Just the index is dropped.

{{{#!sql
BEGIN;
--
-- Alter field artist on album
--
DROP INDEX `foo_album_ca949605` ON `foo_album`;
COMMIT;
}}}

Actual:

The constraint is removed and re-added, which is time consuming since the constraint is an index in itself.

{{{#!sql
BEGIN;
--
-- Alter field artist on album
--
ALTER TABLE `foo_album` DROP FOREIGN KEY `foo_album_artist_id_66b4953c_fk_foo_musician_id`;
DROP INDEX `foo_album_ca949605` ON `foo_album`;
ALTER TABLE `foo_album` ADD CONSTRAINT `foo_album_artist_id_66b4953c_fk_foo_musician_id` FOREIGN KEY (`artist_id`) REFERENCES `foo_musician` (`id`);
COMMIT;
}}}

Additional context:
* The explicit `db_index=False` is needed to work around the duplicate index issue discussed [https://groups.google.com/forum/#!topic/django-developers/3ywugkcaxqs here].
* The duplicate index issue appears to be fixed on master (not sure by what) causing this not to repro there.
* I'm presuming the duplicate index isn't severe enough to warrant backporting, however dropping and re-adding the constraint seems pretty bad, so perhaps more worthy of a backport to 1.10.x?"	Bug	closed	Database layer (models, ORM)	1.10	Normal	fixed	db-indexes, mysql		Accepted	0	0	0	0	0	0
