#28065: makemigrations creates unnecessary migration for adding related_name to
ForeignKey
------------------------------------------+------------------------
               Reporter:  Cameron Dawson  |          Owner:  nobody
                   Type:  Bug             |         Status:  new
              Component:  Migrations      |        Version:  1.11
               Severity:  Normal          |       Keywords:
           Triage Stage:  Unreviewed      |      Has patch:  0
    Needs documentation:  0               |    Needs tests:  0
Patch needs improvement:  0               |  Easy pickings:  0
                  UI/UX:  0               |
------------------------------------------+------------------------
 If you just add a {{{related_name}}} to an existing foreign key, it will
 require a migration which then drops and re-creates the foreign key.  This
 is a non-DB, {{{meta}}} change only and shouldn't create a migration.

 With Models:

 {{{
 class Job(models.Model):
     id = models.BigAutoField(primary_key=True)
     name = models.CharField(max_length=30, null=True)

 class JobDetail(models.Model):
     id = models.BigAutoField(primary_key=True)
     job = models.ForeignKey(Job)
 }}}

 my initial migration is:

 {{{
 operations = [
     migrations.CreateModel(
         name='Job',
         fields=[
             ('id', models.BigAutoField(primary_key=True,
 serialize=False)),
             ('name', models.CharField(max_length=30, null=True)),
         ],
     ),
     migrations.CreateModel(
         name='JobDetail',
         fields=[
             ('id', models.BigAutoField(primary_key=True,
 serialize=False)),
             ('job',
 models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
 to='polls.Job')),
         ],
     ),
 ]
 }}}
 Then if I modify the {{{JobDetail}}} model ForeignKey as follows:
 {{{
 job = models.ForeignKey(Job, related_name="job_details")
 }}}
 and run {{{makemigrations}}}, I end up with a migration:
 {{{
 operations = [
     migrations.AlterField(
         model_name='jobdetail',
         name='job',
 field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
 related_name='job_details', to='polls.Job'),
     ),
 ]
 }}}
 The {{{CREATE_INDEX}}} for both migrations is identical:
 {{{
 CREATE INDEX "polls_jobdetail_job_id_5561ba44" ON "polls_jobdetail"
 ("job_id");
 }}}
 We need to work around it with {{{migrations.RunSQL.noop}}} because the
 tables in our actual application are really large and dropping the index
 and re-creating is very expensive.

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

Reply via email to