Fixed #29897 -- Fixed autodetector's swappable MTI dependency resolution.

Thanks Steven Ganz for the detailed report.
This commit is contained in:
Simon Charette 2018-10-28 18:21:25 -04:00 committed by Tim Graham
parent 82353ef940
commit d8e03fdeb9
2 changed files with 15 additions and 0 deletions

View File

@ -345,6 +345,9 @@ class MigrationAutodetector:
dependency_graph = {op: set() for op in ops} dependency_graph = {op: set() for op in ops}
for op in ops: for op in ops:
for dep in op._auto_deps: for dep in op._auto_deps:
# Resolve intra-app dependencies to handle circular
# references involving a swappable model.
dep = self._resolve_dependency(dep)[0]
if dep[0] == app_label: if dep[0] == app_label:
for op2 in ops: for op2 in ops:
if self.check_dependency(op2, dep): if self.check_dependency(op2, dep):

View File

@ -2348,6 +2348,18 @@ class AutodetectorTests(TestCase):
self.assertOperationTypes(changes, 'a', 0, ["CreateModel"]) self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
self.assertMigrationDependencies(changes, 'a', 0, []) self.assertMigrationDependencies(changes, 'a', 0, [])
@override_settings(AUTH_USER_MODEL='a.User')
def test_swappable_circular_multi_mti(self):
with isolate_lru_cache(apps.get_swappable_settings_name):
parent = ModelState('a', 'Parent', [
('user', models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE))
])
child = ModelState('a', 'Child', [], bases=('a.Parent',))
user = ModelState('a', 'User', [], bases=(AbstractBaseUser, 'a.Child'))
changes = self.get_changes([], [parent, child, user])
self.assertNumberMigrations(changes, 'a', 1)
self.assertOperationTypes(changes, 'a', 0, ['CreateModel', 'CreateModel', 'CreateModel', 'AddField'])
@mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition', @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition',
side_effect=AssertionError("Should not have prompted for not null addition")) side_effect=AssertionError("Should not have prompted for not null addition"))
def test_add_blank_textfield_and_charfield(self, mocked_ask_method): def test_add_blank_textfield_and_charfield(self, mocked_ask_method):