Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.7.x] Fixed #22293 -- Avoided renaming many-to-many tables to thems…

…elves.

Fixed this for both implementations of _alter_many_to_many, instead of
in `alter_db_table` itself (more implementations).

Backport of 1edfa15 from master
  • Loading branch information...
commit 21eaad68e6f26b9f88437c63d584c0a9fb39c9be 1 parent 345281b
@blueyed blueyed authored timgraham committed
View
3  django/db/backends/schema.py
@@ -762,7 +762,8 @@ def _alter_many_to_many(self, model, old_field, new_field, strict):
Alters M2Ms to repoint their to= endpoints.
"""
# Rename the through table
- self.alter_db_table(old_field.rel.through, old_field.rel.through._meta.db_table, new_field.rel.through._meta.db_table)
+ if old_field.rel.through._meta.db_table != new_field.rel.through._meta.db_table:
+ self.alter_db_table(old_field.rel.through, old_field.rel.through._meta.db_table, new_field.rel.through._meta.db_table)
# Repoint the FK to the other side
self.alter_field(
new_field.rel.through,
View
3  django/db/backends/sqlite3/schema.py
@@ -161,6 +161,9 @@ def _alter_many_to_many(self, model, old_field, new_field, strict):
"""
Alters M2Ms to repoint their to= endpoints.
"""
+ if old_field.rel.through == new_field.rel.through:
+ return
+
# Make a new through table
self.create_model(new_field.rel.through)
# Copy the data across
View
9 tests/schema/tests.py
@@ -347,6 +347,15 @@ def test_m2m(self):
# Ensure there is now an m2m table there
columns = self.column_classes(new_field.rel.through)
self.assertEqual(columns['tagm2mtest_id'][0], "IntegerField")
+
+ # "Alter" the field. This should not rename the DB table to itself.
+ with connection.schema_editor() as editor:
+ editor.alter_field(
+ Author,
+ new_field,
+ new_field,
+ )
+
# Remove the M2M table again
with connection.schema_editor() as editor:
editor.remove_field(

0 comments on commit 21eaad6

Please sign in to comment.
Something went wrong with that request. Please try again.