diff --git a/django/db/migrations/optimizer.py b/django/db/migrations/optimizer.py index d31ab89d07..26ac88a221 100644 --- a/django/db/migrations/optimizer.py +++ b/django/db/migrations/optimizer.py @@ -47,9 +47,10 @@ class MigrationOptimizer: in_between = operations[i + 1:i + j + 1] result = operation.reduce(other, in_between, app_label) if isinstance(result, list): - # Optimize! Add result, then remaining others, then return - new_operations.extend(result) + # Add operations optimized through, optimized operations, + # and the remaining ones. new_operations.extend(in_between) + new_operations.extend(result) new_operations.extend(operations[i + j + 2:]) return new_operations if not result: diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 1fde1ba466..5ad32e610c 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -1165,8 +1165,8 @@ class AutodetectorTests(TestCase): # Right number/type of migrations? self.assertNumberMigrations(changes, 'testapp', 1) self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "CreateModel"]) - self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author") - self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher") + self.assertOperationAttributes(changes, "testapp", 0, 0, name="Publisher") + self.assertOperationAttributes(changes, "testapp", 0, 1, name="Author") self.assertMigrationDependencies(changes, 'testapp', 0, [("otherapp", "auto_1")]) # Right number/type of migrations? self.assertNumberMigrations(changes, 'otherapp', 2)