From 33a0a6f033b9243e0cc98e76bd494de73cf144eb Mon Sep 17 00:00:00 2001 From: Johanan Oppong Amoateng Date: Fri, 12 Dec 2025 23:17:38 +0000 Subject: Fixed #36791 -- Made MigrationAutodetector recreate through table when m2m target model changes. Co-Authored-By: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com> Co-Authored-By: Clifford Gama --- django/db/migrations/autodetector.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'django') diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 7cc22c0637..0c2e215fcd 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -1339,7 +1339,10 @@ class MigrationAutodetector: if old_field_dec != new_field_dec and old_field_name == field_name: both_m2m = old_field.many_to_many and new_field.many_to_many neither_m2m = not old_field.many_to_many and not new_field.many_to_many - if both_m2m or neither_m2m: + target_changed = ( + both_m2m and new_field_dec[2]["to"] != old_field_dec[2]["to"] + ) + if (both_m2m or neither_m2m) and not target_changed: # Either both fields are m2m or neither is preserve_default = True if ( -- cgit v1.3