| 1 | diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
|
|---|
| 2 | index d8a042d..f4020e7 100644
|
|---|
| 3 | --- a/tests/migrations/test_operations.py
|
|---|
| 4 | +++ b/tests/migrations/test_operations.py
|
|---|
| 5 | @@ -1659,6 +1659,57 @@ class OperationTests(OperationTestBase):
|
|---|
| 6 | self.assertEqual(definition[1], [])
|
|---|
| 7 | self.assertEqual(sorted(definition[2]), ["atomic", "code"])
|
|---|
| 8 |
|
|---|
| 9 | + def test_run_python_related_assignment(self):
|
|---|
| 10 | +
|
|---|
| 11 | + def inner_method(models, schema_editor):
|
|---|
| 12 | + Author = models.get_model("test_authors", "Author")
|
|---|
| 13 | + Book = models.get_model("test_books", "Book")
|
|---|
| 14 | + author = Author.objects.create(name="Hemingway")
|
|---|
| 15 | + book = Book("Old Man and The Sea")
|
|---|
| 16 | + book.author = author
|
|---|
| 17 | +
|
|---|
| 18 | + create_author = migrations.CreateModel(
|
|---|
| 19 | + "Author",
|
|---|
| 20 | + [
|
|---|
| 21 | + ("id", models.AutoField(primary_key=True)),
|
|---|
| 22 | + ("name", models.CharField(max_length=100)),
|
|---|
| 23 | + ],
|
|---|
| 24 | + options={},
|
|---|
| 25 | + )
|
|---|
| 26 | + create_book = migrations.CreateModel(
|
|---|
| 27 | + "Book",
|
|---|
| 28 | + [
|
|---|
| 29 | + ("id", models.AutoField(primary_key=True)),
|
|---|
| 30 | + ("title", models.CharField(max_length=100)),
|
|---|
| 31 | + ("author", models.ForeignKey("test_authors.Author"))
|
|---|
| 32 | + ],
|
|---|
| 33 | + options={},
|
|---|
| 34 | + )
|
|---|
| 35 | + add_hometown = migrations.AddField(
|
|---|
| 36 | + "Author",
|
|---|
| 37 | + "hometown",
|
|---|
| 38 | + models.CharField(max_length=100),
|
|---|
| 39 | + )
|
|---|
| 40 | + create_old_man = migrations.RunPython(
|
|---|
| 41 | + inner_method, inner_method
|
|---|
| 42 | + )
|
|---|
| 43 | +
|
|---|
| 44 | + project_state = ProjectState()
|
|---|
| 45 | + with connection.schema_editor() as editor:
|
|---|
| 46 | + new_state = project_state.clone()
|
|---|
| 47 | + create_author.state_forwards("test_authors", new_state)
|
|---|
| 48 | + create_author.database_forwards("test_authors", editor, project_state, new_state)
|
|---|
| 49 | + project_state, new_state = new_state, new_state.clone()
|
|---|
| 50 | + create_book.state_forwards("test_books", new_state)
|
|---|
| 51 | + create_book.database_forwards("test_books", editor, project_state, new_state)
|
|---|
| 52 | + project_state, new_state = new_state, new_state.clone()
|
|---|
| 53 | + add_hometown.state_forwards("test_authors", new_state)
|
|---|
| 54 | + add_hometown.database_forwards("test_authors", editor, project_state, new_state)
|
|---|
| 55 | + project_state, new_state = new_state, new_state.clone()
|
|---|
| 56 | + create_old_man.state_forwards("test_books", new_state)
|
|---|
| 57 | + create_old_man.database_forwards("test_books", editor, project_state, new_state)
|
|---|
| 58 | +
|
|---|
| 59 | +
|
|---|
| 60 | def test_run_python_noop(self):
|
|---|
| 61 | """
|
|---|
| 62 | #24098 - Tests no-op RunPython operations.
|
|---|