Opened 7 years ago

Last modified 7 years ago

#28697 closed Bug

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

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

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',
        ),
    ]

Change History (0)

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