Opened 4 years ago

Closed 4 years ago

#32486 closed Uncategorized (duplicate)

Migration which removes UniqueTogether constraint containing a ForeignKey fails with MariaDB (MySQL)

Reported by: Egor Suvorov Owned by: nobody
Component: Uncategorized Version: 3.1
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

This issue closely resembles #24757 to me, but on a recent Django.

Note that it's MySQL-specific, I was unable to reproduce it with SQLite3 backend.

Steps to reproduce

  1. Create an empty project with a MySQL backend, and an app with two models (alternatively, clone my GitHub repo from https://github.com/yeputons/django-unique-constraint-remove-bug and skip makemigrations and editing steps):
from django.db import models

class ModelA(models.Model):
    pass


class ModelB(models.Model):
    a_ptr = models.ForeignKey(ModelA, on_delete=models.CASCADE)
    x = models.IntegerField()

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['a_ptr', 'x'], name='%(app_label)s_%(class)s_unique_a_ptr_x'),  # (1)
        ]
  1. Get the following migration by running python manage.py makemigrations:
# Generated by Django 3.1.3 on 2021-02-27 11:59

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='ModelA',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
        ),
        migrations.CreateModel(
            name='ModelB',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('x', models.IntegerField()),
                ('a_ptr', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='demo_app.modela')),
            ],
        ),
        migrations.AddConstraint(
            model_name='modelb',
            constraint=models.UniqueConstraint(fields=('a_ptr', 'x'), name='demo_app_modelb_unique_a_ptr_x'),
        ),
    ]
  1. Successfully apply the migration by running python manage.py migrate
  2. Then comment out the line (1) above and get the following migration by python manage.py makemigrations:
# Generated by Django 3.1.3 on 2021-02-27 11:59

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('demo_app', '0001_initial'),
    ]

    operations = [
        migrations.RemoveConstraint(
            model_name='modelb',
            name='demo_app_modelb_unique_a_ptr_x',
        ),
    ]
  1. Run python manage.py migrate

Expected result

The migration succeeds and the result is as if there were no UniqueConstraint in the first place.

Actual result

The following error gets reported:

Operations to perform:
  Target specific migration: 0002_auto_20210227_1459, from demo_app
Running migrations:
  Applying demo_app.0002_auto_20210227_1459...Traceback (most recent call last):
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\django\base.py", line 149, in execute
    return self.cursor.execute(query, new_args)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 569, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py", line 651, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py", line 538, in _handle_result
    raise errors.get_exception(packet)
mysql.connector.errors.DatabaseError: 1553 (HY000): Cannot drop index 'demo_app_modelb_unique_a_ptr_x': needed in a foreign key constraint

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\core\management\base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\core\management\commands\migrate.py", line 243, in handle
    post_migrate_state = executor.migrate(
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\migrations\operations\models.py", line 843, in database_forwards
    schema_editor.remove_constraint(model, constraint)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\backends\base\schema.py", line 368, in remove_constraint
    self.execute(sql)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\backends\base\schema.py", line 142, in execute
    cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\django\base.py", line 149, in execute
    return self.cursor.execute(query, new_args)
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\cursor.py", line 569, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py", line 651, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\mysql\connector\connection.py", line 538, in _handle_result
    raise errors.get_exception(packet)
django.db.utils.DatabaseError: (1553, "1553 (HY000): Cannot drop index 'demo_app_modelb_unique_a_ptr_x': needed in a foreign key constraint", 'HY000')

Versions

  • 10.4.14-MariaDB - mariadb.org binary distribution (installed by XAMPP)
  • Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)]
  • django.VERSION == (3, 1, 3, 'final', 0)
  • Windows 7 Pro En x64

Change History (2)

comment:2 by Mariusz Felisiak, 4 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #31335 (see also #31746).

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