﻿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
33392	Removing composed indexes fails on MySQL	Sergey Fursov	nobody	"Removing composed indexes that are being created using UniqueConstraint or Index added to Meta.contraints/Meta.indexes respectively fails on MySQL due to implicit optimization made by MySQL with omitting indexes for a foreign key that were initially included in the index.

The bug might be considered as a regression for the previously fixed #24757

The initial state of example models
{{{
from django.db import models

class ModelA(models.Model):
    pass

class ModelB(models.Model):
    a = models.ForeignKey(MyModelA)
    b = models.IntegerField()

    class Meta:
        constraints = models.UniqueConstraint(fields=['a', 'b'], name='modelb_uniq')
        indexes = models.Index(fields=['a', 'b'], name='modelb_index')
}}}

the final state:

{{{
from django.db import models

class ModelA(models.Model):
    pass

class ModelB(models.Model):
    a = models.ForeignKey(MyModelA)
    b = models.IntegerField()
}}}

considering the example states of the models above, MySQL first optimizes schema by omitting index for ModelB.a FK field as it is already included in composed index, and then during removing the constraint or the index migration fails with 

{{{
Cannot drop index 'modelb_uniq': needed in a foreign key constraint""
}}}

or 

{{{
Cannot drop index 'modelb_index': needed in a foreign key constraint""
}}}"	Bug	closed	Migrations	4.0	Normal	duplicate			Unreviewed	0	0	0	0	0	0
