[Django] #33392: Removing or composed indexes fails on MySQL

7 views
Skip to first unread message

Django

unread,
Dec 28, 2021, 4:14:32 PM12/28/21
to django-...@googlegroups.com
#33392: Removing or composed indexes fails on MySQL
-----------------------------------------+------------------------
Reporter: Sergey Fursov | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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"
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33392>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 28, 2021, 4:17:31 PM12/28/21
to django-...@googlegroups.com
#33392: Removing composed indexes fails on MySQL
-------------------------------+--------------------------------------

Reporter: Sergey Fursov | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/33392#comment:1>

Django

unread,
Dec 29, 2021, 1:40:54 AM12/29/21
to django-...@googlegroups.com
#33392: Removing composed indexes fails on MySQL
-------------------------------+--------------------------------------

Reporter: Sergey Fursov | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 4.0
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

Thanks! IMO we can close this as a duplicate of #31335 (see Simon's
[https://code.djangoproject.com/ticket/31335#comment:2 comment]).

--
Ticket URL: <https://code.djangoproject.com/ticket/33392#comment:2>

Reply all
Reply to author
Forward
0 new messages