#24757: Removing unique_together constraint make Django 1.8 migration fail with
MySQL
-------------------------------+------------------------------
     Reporter:  trecouvr       |      Owner:  nobody
         Type:  Uncategorized  |     Status:  new
    Component:  Migrations     |    Version:  1.8
     Severity:  Normal         |   Keywords:  migrations mysql
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+------------------------------
 Migrations containing `migrations.AlterUniqueTogether(name='xxx',
 unique_together=set([]),)` may fail on Django 1.8 but not on Django 1.7 if
 using the backend mysql.


 Given the database engine is mysql and the following `models.py`:

 {{{
 from django.db import models


 class MyModelA(models.Model):
     s = models.CharField(max_length=120)


 class MyModelB(models.Model):
     a = models.ForeignKey(MyModelA)
     b = models.IntegerField()
 }}}

 And the 2 following migrations:

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

 from django.db import models, migrations


 class Migration(migrations.Migration):

     dependencies = [
     ]

     operations = [
         migrations.CreateModel(
             name='MyModelA',
             fields=[
                 ('id', models.AutoField(serialize=False,
 auto_created=True, verbose_name='ID', primary_key=True)),
                 ('s', models.CharField(max_length=120)),
             ],
         ),
         migrations.CreateModel(
             name='MyModelB',
             fields=[
                 ('id', models.AutoField(serialize=False,
 auto_created=True, verbose_name='ID', primary_key=True)),
                 ('b', models.IntegerField()),
                 ('a', models.ForeignKey(to='mysite.MyModelA')),
             ],
         ),
         migrations.AlterUniqueTogether(
             name='mymodelb',
             unique_together=set([('a', 'b')]),
         ),
     ]
 }}}

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

 from django.db import models, migrations


 class Migration(migrations.Migration):

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

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


 Running `./manage.py migrate` works fine on Django 1.7, but on Django 1.8
 I get the following error:

 {{{
 $ ./manage.py migrate
 Operations to perform:
   Synchronize unmigrated apps: staticfiles, messages
   Apply all migrations: contenttypes, auth, mysite, sessions, admin
 Synchronizing apps without migrations:
   Creating tables...
     Running deferred SQL...
   Installing custom SQL...
 Running migrations:
   Rendering model states... DONE
   [..]
   Applying mysite.0001_initial... OK
   Applying mysite.0002_auto...Traceback (most recent call last):
   [..]
 django.db.utils.OperationalError: (1553, "Cannot drop index
 'mysite_mymodelb_a_id_b53c781c93aab9a_uniq': needed in a foreign key
 constraint")
 }}}


 The same migrations work on psql, I did not have the chance to test it
 against sqlite.

 Inspecting the generated sql I found Django 1.7 add an index in addition
 to the unique constraint and Django 1.8 does not.

 On Django 1.7

 {{{
 BEGIN;
 CREATE TABLE `mysite_mymodela` (`id` integer AUTO_INCREMENT NOT NULL
 PRIMARY KEY, `s` varchar(120) NOT NULL);
 CREATE TABLE `mysite_mymodelb` (`id` integer AUTO_INCREMENT NOT NULL
 PRIMARY KEY, `b` integer NOT NULL, `a_id` integer NOT NULL);
 ALTER TABLE `mysite_mymodelb` ADD CONSTRAINT
 `mysite_mymodelb_a_id_48275010b87402ed_uniq` UNIQUE (`a_id`, `b`);
 ALTER TABLE `mysite_mymodelb` ADD CONSTRAINT
 `mysite_mymodelb_a_id_4981d3920f0fef1e_fk_mysite_mymodela_id` FOREIGN KEY
 (`a_id`) REFERENCES `mysite_mymodela` (`id`);
 CREATE INDEX `mysite_mymodelb_e2b453f4` ON `mysite_mymodelb` (`a_id`);

 COMMIT;
 }}}

 Note the {{{CREATE INDEX `mysite_mymodelb_e2b453f4` ON `mysite_mymodelb`
 (`a_id`);}}}.

 And on Django 1.8
 {{{
 BEGIN;
 CREATE TABLE `mysite_mymodela` (`id` integer AUTO_INCREMENT NOT NULL
 PRIMARY KEY, `s` varchar(120) NOT NULL);
 CREATE TABLE `mysite_mymodelb` (`id` integer AUTO_INCREMENT NOT NULL
 PRIMARY KEY, `b` integer NOT NULL, `a_id` integer NOT NULL);
 ALTER TABLE `mysite_mymodelb` ADD CONSTRAINT
 `mysite_mymodelb_a_id_784bb266d0e363ab_uniq` UNIQUE (`a_id`, `b`);
 ALTER TABLE `mysite_mymodelb` ADD CONSTRAINT
 `mysite_mymodelb_a_id_50892c78e36678b8_fk_mysite_mymodela_id` FOREIGN KEY
 (`a_id`) REFERENCES `mysite_mymodela` (`id`);

 COMMIT;
 }}}

 I joined the full traceback.

--
Ticket URL: <https://code.djangoproject.com/ticket/24757>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.dad297614655e202a9a0278d8c7fdf28%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to