Fixed #36168: Handled backward migration squash bug. #19612
Draft
+371
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Trac ticket number
ticket-#36168
Branch description
This branch fixes a bug affecting backward migrations to replaced migrations in projects with squashed migrations spanning multiple apps.
The core issue was that migration_plan() mutates the global migration loader’s graph to disable replacement pruning, which impacts subsequent calls like _create_project_state(). This causes the migration graph to incorrectly include both squashed and replaced migrations as leaf nodes, leading to duplicate state mutations and errors such as FieldDoesNotExist.
The _migrate_all_backwards() method is updated to avoid relying on the mutated global loader by using a temporary loader instance with replace_migrations=False to safely generate migration plans without side effects.
This prevents global state mutation, ensures consistent project state reconstruction during backward migrations, and resolves related migration errors.
Checklist
main
branch.