#25253: Migrations do unecessary work when adding `blank=True` to M2M field on
MySQL
----------------------------+----------------------------------
     Reporter:  trecouvr    |      Owner:  nobody
         Type:  Bug         |     Status:  new
    Component:  Migrations  |    Version:  1.8
     Severity:  Normal      |   Keywords:  migrations m2m mysql
 Triage Stage:  Unreviewed  |  Has patch:  0
Easy pickings:  0           |      UI/UX:  0
----------------------------+----------------------------------
 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/25253>
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/051.afa4f0f63c0d12dc2b20ae00c6761d97%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to