#27768: makemigrations uses unnecessary AddField for ForeignKey depending on 
model
name
----------------------------+--------------------------------------
     Reporter:  Ed Morley   |                    Owner:  nobody
         Type:  Bug         |                   Status:  new
    Component:  Migrations  |                  Version:  master
     Severity:  Normal      |               Resolution:
     Keywords:              |             Triage Stage:  Unreviewed
    Has patch:  0           |      Needs documentation:  0
  Needs tests:  0           |  Patch needs improvement:  0
Easy pickings:  0           |                    UI/UX:  0
----------------------------+--------------------------------------

Comment (by Ed Morley):

 I guess there are two parts to this:

 1) When performing a `makemigrations` to generate the initial migrations
 file, if the order that the models were declared in models.py was
 preserved, then the resultant operations would (a) likely require less
 optimization in the first place (since unless people use string references
 to other models, they will be declared in the correct order in the file),
 and (b) users could at least control the order.

 2) Ideally the migrations optimizer would reorder `CreateModel`s (if no
 other dependencies between them) rather than refuse to optimize across
 them.

 ie for (2),
 
[https://github.com/django/django/blob/e5c2e43cc832028a974399af07a1c3ba6afa2467/tests/migrations/test_optimizer.py#L313-L329
 this existing test]:

 {{{#!python
     def test_create_model_add_field_not_through_fk(self):
         """
         AddField should NOT optimize into CreateModel if it's an FK to a
 model
         that's between them.
         """
         self.assertOptimizesTo(
             [
                 migrations.CreateModel("Foo", [("name",
 models.CharField(max_length=255))]),
                 migrations.CreateModel("Link", [("url",
 models.TextField())]),
                 migrations.AddField("Foo", "link",
 models.ForeignKey("migrations.Link", models.CASCADE)),
             ],
             [
                 migrations.CreateModel("Foo", [("name",
 models.CharField(max_length=255))]),
                 migrations.CreateModel("Link", [("url",
 models.TextField())]),
                 migrations.AddField("Foo", "link",
 models.ForeignKey("migrations.Link", models.CASCADE)),
             ],
         )
 }}}

 Should actually be changed to:

 {{{#!python
         self.assertOptimizesTo(
             [
                 migrations.CreateModel("Foo", [("name",
 models.CharField(max_length=255))]),
                 migrations.CreateModel("Link", [("url",
 models.TextField())]),
                 migrations.AddField("Foo", "link",
 models.ForeignKey("migrations.Link", models.CASCADE)),
             ],
             [
                 migrations.CreateModel("Link", [("url",
 models.TextField())]),
                 migrations.CreateModel("Foo", [("name",
 models.CharField(max_length=255)), ("link",
 models.ForeignKey("migrations.Link", models.CASCADE)]),
             ],
         )
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27768#comment:1>
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/066.ce425fb4496070e5c1f36aa89a77ba01%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to