1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

[4.1.x] Fixed #31788 -- Fixed migration optimization after altering field to ManyToManyField.

This makes AddField() used for altering to ManyToManyField, dependent
on the prior RemoveField.
Backport of 798b6c23ee from main
This commit is contained in:
David Wobrock
2022-06-02 12:10:27 +02:00
committed by Mariusz Felisiak
parent 0fb0355271
commit 9fce76a237
2 changed files with 49 additions and 6 deletions

View File

@@ -1022,8 +1022,9 @@ class MigrationAutodetector:
def _generate_added_field(self, app_label, model_name, field_name):
field = self.to_state.models[app_label, model_name].get_field(field_name)
# Fields that are foreignkeys/m2ms depend on stuff
dependencies = []
# Adding a field always depends at least on its removal.
dependencies = [(app_label, model_name, field_name, False)]
# Fields that are foreignkeys/m2ms depend on stuff.
if field.remote_field and field.remote_field.model:
dependencies.extend(
self._get_dependencies_for_foreign_key(