Opened 7 years ago

Last modified 7 years ago

#28697 closed Bug

Removing unique_together constraint make migration fail with MySQL — at Version 1

Reported by: Esteban C Borsani Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords: migrations
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Esteban C Borsani)

I have found Django 1.8.18 fails when removing a model containing a unique_together constraint. Django 1.11 works fine. I'm running Python 3.6, mysql 5.7 and using the mysqlclient connector.

These are actually two errors:

  • When removing just the constraint, the error is ValueError: Found wrong number (0) of constraints for myapp_topicpollvote(user_id, choice_id)
  • When removing everything altogether the error is django.db.utils.OperationalError: (1553, "Cannot drop index 'myapp_topicpollch_poll_id_5bee0f72ec1f3a69_fk_myapp_topicpoll_id': needed in a foreign key constraint")

Here is the model

# -*- coding: utf-8 -*-

from django.db import models
from django.conf import settings


class TopicPoll(models.Model):
    """"""


class TopicPollChoice(models.Model):

    poll = models.ForeignKey(TopicPoll, on_delete=models.CASCADE)


class TopicPollVote(models.Model):

    choice = models.ForeignKey(TopicPollChoice, on_delete=models.CASCADE)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)

    class Meta:
        # removing this raises:
        # ValueError: Found wrong number (0) of constraints for myapp_topicpollvote(user_id, choice_id)

        # removing all models raises
        # django.db.utils.OperationalError: (1553, "Cannot drop index ...
        unique_together = (('user', 'choice'),)

Initial migration:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='TopicPoll',
            fields=[
                ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
            ],
        ),
        migrations.CreateModel(
            name='TopicPollChoice',
            fields=[
                ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
                ('poll', models.ForeignKey(to='myapp.TopicPoll')),
            ],
        ),
        migrations.CreateModel(
            name='TopicPollVote',
            fields=[
                ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
                ('choice', models.ForeignKey(to='myapp.TopicPollChoice')),
                ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ],
        ),
        migrations.AlterUniqueTogether(
            name='topicpollvote',
            unique_together=set([('user', 'choice')]),
        ),
    ]

Removing just the unique_together:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterUniqueTogether(
            name='topicpollvote',
            unique_together=set([]),
        ),
    ]

Removing everything altogether (unique_together + all models):

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

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

    operations = [
        migrations.RemoveField(
            model_name='topicpollchoice',
            name='poll',
        ),
        migrations.AlterUniqueTogether(
            name='topicpollvote',
            unique_together=set([]),
        ),
        migrations.RemoveField(
            model_name='topicpollvote',
            name='choice',
        ),
        migrations.RemoveField(
            model_name='topicpollvote',
            name='user',
        ),
        migrations.DeleteModel(
            name='TopicPoll',
        ),
        migrations.DeleteModel(
            name='TopicPollChoice',
        ),
        migrations.DeleteModel(
            name='TopicPollVote',
        ),
    ]

Tracebacks:

  Applying myapp.0002_auto_20171011_0311...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/db/migrations/operations/models.py", line 359, in database_forwards
    getattr(new_model._meta, self.option_name, set()),
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/db/backends/base/schema.py", line 320, in alter_unique_together
    self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/db/backends/mysql/schema.py", line 80, in _delete_composed_index
    return super(DatabaseSchemaEditor, self)._delete_composed_index(model, fields, *args)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/db/backends/base/schema.py", line 349, in _delete_composed_index
    ", ".join(columns),
ValueError: Found wrong number (0) of constraints for myapp_topicpollvote(user_id, choice_id)

The other one:

Applying myapp.0002_auto_20171011_0259...Traceback (most recent call last):
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
  File "/home/esteban/PycharmProjects/venv35/lib64/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1553, "Cannot drop index 'myapp_topicpollch_poll_id_5bee0f72ec1f3a69_fk_myapp_topicpoll_id': needed in a foreign key constraint")

Change History (1)

comment:1 by Esteban C Borsani, 7 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top