diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index d8a042d..7bcdf5d 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -112,7 +112,7 @@ class OperationTestBase(MigrationTestBase):
                 [
                     ("id", models.AutoField(primary_key=True)),
                     ("pony", models.ForeignKey("Pony")),
-                    ("friend", models.ForeignKey("self"))
+                    ("friend", models.ForeignKey("self", null=True))
                 ],
             ))
         if mti_model:
@@ -1538,7 +1538,7 @@ class OperationTests(OperationTestBase):
         Tests the RunPython operation
         """
 
-        project_state = self.set_up_test_model("test_runpython", mti_model=True)
+        project_state = self.set_up_test_model("test_runpython", mti_model=True, related_model=True)
 
         # Create the operation
         def inner_method(models, schema_editor):
@@ -1616,6 +1616,23 @@ class OperationTests(OperationTestBase):
         self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 6)
         self.assertEqual(project_state.apps.get_model("test_runpython", "ShetlandPony").objects.count(), 2)
 
+    def test_run_python_with_related_objects(self):
+        project_state = self.set_up_test_model("test_runpython_with_rel_objects", related_model=True)
+        new_state = project_state.clone()
+
+        def create_pony_with_rider(models, schema_editor):
+            Pony = models.get_model("test_runpython_with_rel_objects", "Pony")
+            Rider = models.get_model("test_runpython_with_rel_objects", "Rider")
+            pony_obj = Pony.objects.create(weight=13.13)
+            rider = Rider(pony=pony_obj)
+            rider.save()
+
+        operation = migrations.RunPython(create_pony_with_rider)
+        with connection.schema_editor() as editor:
+            operation.database_forwards("test_runpython_with_rel_objects", editor, project_state, new_state)
+        self.assertEqual(project_state.apps.get_model("test_runpython_with_rel_objects", "Pony").objects.count(), 1)
+        self.assertEqual(project_state.apps.get_model("test_runpython_with_rel_objects", "Rider").objects.count(), 1)
+
     def test_run_python_atomic(self):
         """
         Tests the RunPython operation correctly handles the "atomic" keyword
