﻿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
31746	UniqueConstraint with ForeignKey cannot be removed on MySQL/MariaDB	Christopher Currie	nobody	"Migrations containing `migrations.AddConstraint(model_name='xxx', constraint=models.UniqueConstraint(fields=[], name='yyy'))` will fail when unapplied on MySQL/MariaDB backends.

Given the database engine is mysql and the following `models.py`:

{{{
from django.db import models


class Parent(models.Model):
    name = models.CharField(max_length=30)


class Child(models.Model):
    parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
    name = models.CharField(max_length=30)
}}}

And the 2 following migrations:

{{{
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('data', '0001_initial'),
    ]

    operations = [
        migrations.AddConstraint(
            model_name='child',
            constraint=models.UniqueConstraint(fields=('parent', 'name'), name='unique_parent_child_name'),
        ),
    ]
}}}
{{{
class Migration(migrations.Migration):

    dependencies = [
        ('data', '0002_auto_20200627_1823'),
    ]

    operations = [
        migrations.RemoveConstraint(
            model_name='child',
            name='unique_parent_child_name',
        ),
    ]
}}}

Running `./manage.py migrate` produces the following error: 
{{{
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, data, sessions
Running migrations:
  Applying data.0003_auto_20200627_1838...Traceback (most recent call last):
  [...]
MySQLdb._exceptions.OperationalError: (1553, ""Cannot drop index 'unique_parent_child_name': needed in a foreign key constraint"")
}}}

The same migrations work against sqlite. The error reproduces against mariadb 10, mysql 8, and mysql 5, with Django versions 2.2 and 3.0.

This issue was previously reported for Django 1.8:

https://code.djangoproject.com/ticket/24757

That issue was resolved against the deprecated `unique_together` form. Using `unique_together` in place of UniqueConstraint works as expected.

A complete project reproducing this issue is attached.
"	Bug	closed	Migrations	3.0	Normal	duplicate			Unreviewed	0	0	0	0	0	0
