Skip to content

Commit

Permalink
Fixed #34820 -- Migrations crashes when changing ForeignObject proper…
Browse files Browse the repository at this point in the history
…ties.
  • Loading branch information
RelaxedDong committed Sep 9, 2023
1 parent 1ab2cf7 commit 619c785
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ answer newbie questions, and generally made Django that much better:
Hang Park <hangpark@kaist.ac.kr>
Hannes Ljungberg <hannes.ljungberg@gmail.com>
Hannes Struß <x@hannesstruss.de>
Hao Dong <https://github.com/RelaxedDong>
Harm Geerts <hgeerts@gmail.com>
Hasan Ramezani <hasan.r67@gmail.com>
Hawkeye
Expand Down
2 changes: 2 additions & 0 deletions django/db/backends/base/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,8 @@ def _field_indexes_sql(self, model, field):
return output

def _field_should_be_altered(self, old_field, new_field, ignore=None):
if not old_field.concrete and not new_field.concrete:
return False
ignore = ignore or set()
_, old_path, old_args, old_kwargs = old_field.deconstruct()
_, new_path, new_args, new_kwargs = new_field.deconstruct()
Expand Down
50 changes: 50 additions & 0 deletions tests/migrations/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,56 @@ def test_alter_field_add_db_column_noop(self):
operation.database_forwards(app_label, editor, new_state, project_state)
self.assertColumnExists(rider_table, "pony_id")

def test_alter_field_foreignobject_noop(self):
try:
with self.assertNumQueries(0), connection.schema_editor() as editor:
pass
except Exception:
# fetch query count
setup_database_queries_count = len(connection.queries)
self.assertTrue(setup_database_queries_count > 0)
app_label = "test_alflfo_noop"
project_state = self.set_up_test_model(app_label)
project_state = self.apply_operations(
app_label,
project_state,
[
migrations.CreateModel(
"Rider",
fields=[
("pony_id", models.IntegerField()),
(
"pony",
models.ForeignObject(
f"{app_label}.Pony",
models.CASCADE,
from_fields=("pony_id",),
to_fields=("id",),
),
),
],
),
],
)
operation = migrations.AlterField(
"Rider",
"pony",
models.ForeignObject(
f"{app_label}.Pony",
models.CASCADE,
from_fields=("pony_id",),
to_fields=("id",),
null=True,
),
)
new_state = project_state.clone()
operation.state_forwards(app_label, new_state)
operation.database_forwards(app_label, editor, project_state, new_state)
with self.assertNumQueries(
setup_database_queries_count
), connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state, new_state)

@skipUnlessDBFeature("supports_comments")
def test_alter_model_table_comment(self):
app_label = "test_almotaco"
Expand Down

0 comments on commit 619c785

Please sign in to comment.