﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25253	MySQL migrations drop & recreate constraints unnecessarily when changing attributes that don't affect the schema	Thomas Recouvreux	Çağıl Uluşahin	"When running a MySQL migration to add `blank=True` on a `ManyToManyField`, Django drops the FK constraints on the join table and creates them back.


Here is the changeset for models.py:

{{{
from django.contrib.auth.models import User
from django.db import models


class Banana(models.Model):
-    users = models.ManyToManyField(User)
+    users = models.ManyToManyField(User, blank=True)
}}}


And here the corresponding migration:

{{{
..
    operations = [
        migrations.AlterField(
            model_name='banana',
            name='users',
            field=models.ManyToManyField(to=settings.AUTH_USER_MODEL, blank=True),
        ),
    ]
..
}}}



The resulting sql for MySQL backend is:

{{{
ALTER TABLE `myproject_banana_users` DROP FOREIGN KEY `myproject_banana_users_user_id_93c9ecfd1bc0dc2_fk_auth_user_id`;
ALTER TABLE `myproject_banana_users` ADD CONSTRAINT `myproject_banana_users_user_id_60a2450416dd445f_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
ALTER TABLE `myproject_banana_users` DROP FOREIGN KEY `myproject_bana_banana_id_4be506ef34515098_fk_myproject_banana_id`;
ALTER TABLE `myproject_banana_users` ADD CONSTRAINT `myproject_bana_banana_id_43426c15f7d142f5_fk_myproject_banana_id` FOREIGN KEY (`banana_id`) REFERENCES `myproject_banana` (`id`);
}}}

I checked on PostgreSQL the resulting sql is empty, which looks good to me since there is no change to issue to the database."	Cleanup/optimization	closed	Migrations	2.1	Normal	fixed	migrations m2m mysql	Phil Krylov Adam Johnson	Ready for checkin	1	0	0	0	0	0
