From b2407e4d7d25e4a5839bb512cb2fdf71e9e30a21 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sun, 8 Jun 2025 16:56:46 -0400 Subject: Fixed #35305 -- Avoided recreating constraints on fields renamed via db_column. --- django/db/migrations/autodetector.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'django') diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 13fe8f01a4..1d25101219 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -1460,6 +1460,29 @@ class MigrationAutodetector: for attr in new_constraint.non_db_attrs: new_kwargs.pop(attr, None) + # Replace renamed fields if the db_column is preserved. + for ( + _, + _, + rem_db_column, + rem_field_name, + _, + _, + field, + field_name, + ) in self.renamed_operations: + if field.db_column and rem_db_column == field.db_column: + new_fields = new_kwargs["fields"] + try: + new_field_idx = new_fields.index(field_name) + except ValueError: + continue + new_kwargs["fields"] = tuple( + new_fields[:new_field_idx] + + (rem_field_name,) + + new_fields[new_field_idx + 1 :] + ) + return (old_path, old_args, old_kwargs) != (new_path, new_args, new_kwargs) def create_altered_constraints(self): -- cgit v1.3