Opened 4 years ago

Closed 4 years ago

#31746 closed Bug (duplicate)

UniqueConstraint with ForeignKey cannot be removed on MySQL/MariaDB

Reported by: Christopher Currie Owned by: nobody
Component: Migrations Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Attachments (1)

django_mysql_bug.zip (7.6 KB ) - added by Christopher Currie 4 years ago.
Project reproducing error

Download all attachments as: .zip

Change History (2)

by Christopher Currie, 4 years ago

Attachment: django_mysql_bug.zip added

Project reproducing error

comment:1 by Mariusz Felisiak, 4 years ago

Component: UncategorizedMigrations
Resolution: duplicate
Status: newclosed
Summary: Regression: UniqueConstraint with ForeignKey cannot be removed on MySQL/MariaDBUniqueConstraint with ForeignKey cannot be removed on MySQL/MariaDB
Type: UncategorizedBug

Thanks! IMO we can close this as a duplicate of #31335 (see Simon's comments).

Note: See TracTickets for help on using tickets.
Back to Top