diff options
| author | Clifford Gama <cliffygamy@gmail.com> | 2025-12-15 23:36:24 +0200 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-12-16 17:47:10 -0500 |
| commit | ba7f49c32118ecbe03c53414c71caafd2d5d2bd2 (patch) | |
| tree | 7795acf0a3e1ccb03b643e3c34e7790d70d1092c /django | |
| parent | be26ac85fdf06a7a8a655f6e7000b1263890717d (diff) | |
[6.0.x] Fixed #36800 -- Restored ManyToManyField renaming in BaseDatabaseSchemaEditor.alter_field().
Regression in f9a44cc0fac653f8e0c2ab1cdfb12b2cc5c63fc2.
Now that ManyToManyField is no longer concrete the decision of whether or not
it should be altered, which is also relied on by field renaming, should take
into consideration name changes even if it doesn't have a column associated
with it, as auto-created many-to-many relationship table names are a base of it.
Note that there is room for optimization here where a rename can be entirely
avoided if ManyToManyField.db_table remains stable between .name changes, just
like we do with Field.db_column remaining stable, but since this is a
regression and meant to be backported the current patch focuses on correctness
over further improvements.
Thanks Josik for the report.
Co-authored-by: Simon Charette <charette.s@gmail.com>
Backport of 6cc1231285a20b11058143f8cb0a6b4b3999b23a from main.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/backends/base/schema.py | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 96d555f862..65449afdc8 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -1668,7 +1668,7 @@ class BaseDatabaseSchemaEditor: _, old_path, old_args, old_kwargs = old_field.deconstruct() _, new_path, new_args, new_kwargs = new_field.deconstruct() # Don't alter when: - # - changing only a field name + # - changing only a field name (unless it's a many-to-many) # - changing an attribute that doesn't affect the schema # - changing an attribute in the provided set of ignored attributes # - adding only a db_column and the column name is not changed @@ -1686,7 +1686,7 @@ class BaseDatabaseSchemaEditor: ): old_kwargs.pop("to", None) new_kwargs.pop("to", None) - # db_default can take many form but result in the same SQL. + # db_default can take many forms but result in the same SQL. if ( old_kwargs.get("db_default") and new_kwargs.get("db_default") @@ -1694,11 +1694,19 @@ class BaseDatabaseSchemaEditor: ): old_kwargs.pop("db_default") new_kwargs.pop("db_default") - return ( + if ( old_field.concrete and new_field.concrete and (self.quote_name(old_field.column) != self.quote_name(new_field.column)) - ) or (old_path, old_args, old_kwargs) != (new_path, new_args, new_kwargs) + ): + return True + if ( + old_field.many_to_many + and new_field.many_to_many + and old_field.name != new_field.name + ): + return True + return (old_path, old_args, old_kwargs) != (new_path, new_args, new_kwargs) def _field_should_be_indexed(self, model, field): return field.db_index and not field.unique |
