#24282: Cannot Modify Foreign Keys Within Data Migrations
---------------------------------+-------------------------------------
     Reporter:  jeffasinger      |                    Owner:
         Type:  Bug              |                   Status:  new
    Component:  Migrations       |                  Version:  1.8alpha1
     Severity:  Release blocker  |               Resolution:
     Keywords:                   |             Triage Stage:  Accepted
    Has patch:  0                |      Needs documentation:  0
  Needs tests:  0                |  Patch needs improvement:  0
Easy pickings:  0                |                    UI/UX:  0
---------------------------------+-------------------------------------

Comment (by jeffasinger):

 I've created a test that fails.

 It creates two models, in two seperate apps, and then alters the one with
 a ForeignKey pointing at it before running the DataMigration.

 {{{
 def inner_method(models, schema_editor):
         Author = models.get_model("test_authors", "Author")
         Book = models.get_model("test_books", "Book")
         author = Author.objects.create(name="Hemingway")
         book = Book("Old Man and The Sea")
         book.author = author

     create_author = migrations.CreateModel(
         "Author",
         [
             ("id", models.AutoField(primary_key=True)),
             ("name", models.CharField(max_length=100)),
         ],
         options={},
     )
     create_book = migrations.CreateModel(
         "Book",
         [
             ("id", models.AutoField(primary_key=True)),
             ("title", models.CharField(max_length=100)),
             ("author", models.ForeignKey("test_authors.Author"))
         ],
         options={},
     )
     add_hometown = migrations.AddField(
         "Author",
         "hometown",
         models.CharField(max_length=100),
     )
     create_old_man = migrations.RunPython(
         inner_method, inner_method
     )

     project_state = ProjectState()
     with connection.schema_editor() as editor:
         new_state = project_state.clone()
         create_author.state_forwards("test_authors", new_state)
         create_author.database_forwards("test_authors", editor,
 project_state, new_state)
         project_state, new_state = new_state, new_state.clone()
         create_book.state_forwards("test_books", new_state)
         create_book.database_forwards("test_books", editor, project_state,
 new_state)
         project_state, new_state = new_state, new_state.clone()
         add_hometown.state_forwards("test_authors", new_state)
         add_hometown.database_forwards("test_authors", editor,
 project_state, new_state)
         project_state, new_state = new_state, new_state.clone()
         create_old_man.state_forwards("test_books", new_state)
         create_old_man.database_forwards("test_books", editor,
 project_state, new_state)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24282#comment:9>
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/069.9c1727af14e652b7ac4efdaf0a8e857d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to