#25253: MySQL migrations drop & recreate constraints unnecessarily when changing
attributes that don't affect the schema
--------------------------------------+------------------------------------
     Reporter:  Thomas Recouvreux     |                    Owner:  Shun Yu
         Type:  Cleanup/optimization  |                   Status:  assigned
    Component:  Migrations            |                  Version:  1.8
     Severity:  Normal                |               Resolution:
     Keywords:  migrations m2m mysql  |             Triage Stage:  Accepted
    Has patch:  0                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------

Comment (by Shun Yu):

 Just for clarity, this seems to be a MySQL specific symptom. I tried using
 PostgreSQL 9.5.8 and Django 2.0.dev and could not replicate the add/drop
 of indices.

 {{{
 from django.db import models
 from django.contrib.auth.models import User

 class Banana(models.Model):
     - users = models.ForeignKey(User, on_delete=models.CASCADE)
     + users = models.ForeignKey(User, on_delete=models.PROTECT)
 }}}

 {{{
 class Migration(migrations.Migration):

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

     operations = [
         migrations.AlterField(
             model_name='banana',
             name='users',
 field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT,
 to=settings.AUTH_USER_MODEL),
         ),
     ]
 }}}

 {{{
 $ python manage.py sqlmigrate --database mysql banana 0001
 BEGIN;
 --
 -- Create model Banana
 --
 CREATE TABLE `banana_banana` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY
 KEY, `users_id` integer NOT NULL);
 ALTER TABLE `banana_banana` ADD CONSTRAINT
 `banana_banana_users_id_b9f5715e_fk_auth_user_id` FOREIGN KEY (`users_id`)
 REFERENCES `auth_user` (`id`);
 COMMIT;

 $ python manage.py sqlmigrate --database mysql banana 0002
 BEGIN;
 --
 -- Alter field users on banana
 --
 ALTER TABLE `banana_banana` DROP FOREIGN KEY
 `banana_banana_users_id_b9f5715e_fk_auth_user_id`;
 ALTER TABLE `banana_banana` ADD CONSTRAINT
 `banana_banana_users_id_b9f5715e_fk_auth_user_id` FOREIGN KEY (`users_id`)
 REFERENCES `auth_user` (`id`);
 COMMIT;
 }}}

 {{{
 $ python manage.py sqlmigrate --database postgres banana 0001
 BEGIN;
 --
 -- Create model Banana
 --
 CREATE TABLE "banana_banana" ("id" serial NOT NULL PRIMARY KEY, "users_id"
 integer NOT NULL);
 ALTER TABLE "banana_banana" ADD CONSTRAINT
 "banana_banana_users_id_b9f5715e_fk_auth_user_id" FOREIGN KEY ("users_id")
 REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED;
 CREATE INDEX "banana_banana_users_id_b9f5715e" ON "banana_banana"
 ("users_id");
 COMMIT;

 $ python manage.py sqlmigrate --database postgres banana 0002
 BEGIN;
 --
 -- Alter field users on banana
 --
 COMMIT;
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/25253#comment:12>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.1f1b7516e7c171280dd460932846858c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to