Skip to content

Commit

Permalink
Fixed #34881 -- Fixed a crash when renaming a model with multiple Man…
Browse files Browse the repository at this point in the history
…yToManyField.through references on SQLite.

Thank you to dennisvang for the report and Jase Hackman for the test.

Co-authored-by: Jase Hackman <[email protected]>
  • Loading branch information
2 people authored and sarahboyce committed Jun 13, 2024
1 parent d28626e commit e99187e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/db/migrations/operations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,11 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
model = new_model
related_key = (app_label, self.new_name_lower)
else:
model = related_object.related_model
related_key = (
related_object.related_model._meta.app_label,
related_object.related_model._meta.model_name,
)
model = to_state.apps.get_model(*related_key)
to_field = to_state.apps.get_model(*related_key)._meta.get_field(
related_object.field.name
)
Expand Down
83 changes: 83 additions & 0 deletions tests/migrations/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,89 @@ def test_rename_m2m_model_after_rename_field(self):
ponyrider = PonyRider.objects.create()
ponyrider.riders.add(jockey)

def test_rename_m2m_field_with_2_references(self):
app_label = "test_rename_multiple_references"
project_state = self.apply_operations(
app_label,
ProjectState(),
operations=[
migrations.CreateModel(
name="Person",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name="Relation",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"child",
models.ForeignKey(
on_delete=models.CASCADE,
related_name="relations_as_child",
to="test_rename_multiple_references.person",
),
),
(
"parent",
models.ForeignKey(
on_delete=models.CASCADE,
related_name="relations_as_parent",
to="test_rename_multiple_references.person",
),
),
],
),
migrations.AddField(
model_name="person",
name="parents_or_children",
field=models.ManyToManyField(
blank=True,
through="test_rename_multiple_references.Relation",
to="test_rename_multiple_references.person",
),
),
],
)
Person = project_state.apps.get_model(app_label, "Person")
Relation = project_state.apps.get_model(app_label, "Relation")

person1 = Person.objects.create(name="John Doe")
person2 = Person.objects.create(name="Jane Smith")
Relation.objects.create(child=person2, parent=person1)

self.assertTableExists(app_label + "_person")
self.assertTableNotExists(app_label + "_personfoo")

self.apply_operations(
app_label,
project_state,
operations=[
migrations.RenameModel(old_name="Person", new_name="PersonFoo"),
],
)

self.assertTableNotExists(app_label + "_person")
self.assertTableExists(app_label + "_personfoo")

def test_add_field(self):
"""
Tests the AddField operation.
Expand Down

0 comments on commit e99187e

Please sign in to comment.