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)
Change History (2)
by , 4 years ago
Attachment: | django_mysql_bug.zip added |
---|
comment:1 by , 4 years ago
Component: | Uncategorized → Migrations |
---|---|
Resolution: | → duplicate |
Status: | new → closed |
Summary: | Regression: UniqueConstraint with ForeignKey cannot be removed on MySQL/MariaDB → UniqueConstraint with ForeignKey cannot be removed on MySQL/MariaDB |
Type: | Uncategorized → Bug |
Project reproducing error