diff --git a/django/db/migrations/migration.py b/django/db/migrations/migration.py index 08fdd1914f..b9f4f8f88c 100644 --- a/django/db/migrations/migration.py +++ b/django/db/migrations/migration.py @@ -192,7 +192,7 @@ class Migration: ): name = '_'.join(sorted(o.migration_name_fragment for o in self.operations)) if name is None: - name = 'auto_%s' % get_migration_name_timestamp() + name = 'initial' if self.initial else 'auto_%s' % get_migration_name_timestamp() return name diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index d6afd85273..236af43834 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -612,6 +612,26 @@ class AutodetectorTests(TestCase): self.assertEqual(changes["otherapp"][0].name, "0002_pony_stable") self.assertEqual(changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")]) + def test_arrange_for_graph_with_multiple_initial(self): + # Make a fake graph. + graph = MigrationGraph() + # Use project state to make a new migration change set. + before = self.make_project_state([]) + after = self.make_project_state([self.author_with_book, self.book, self.attribution]) + autodetector = MigrationAutodetector(before, after, MigrationQuestioner({'ask_initial': True})) + changes = autodetector._detect_changes() + changes = autodetector.arrange_for_graph(changes, graph) + + self.assertEqual(changes['otherapp'][0].name, '0001_initial') + self.assertEqual(changes['otherapp'][0].dependencies, []) + self.assertEqual(changes['otherapp'][1].name, '0002_initial') + self.assertCountEqual( + changes['otherapp'][1].dependencies, + [('testapp', '0001_initial'), ('otherapp', '0001_initial')], + ) + self.assertEqual(changes['testapp'][0].name, '0001_initial') + self.assertEqual(changes['testapp'][0].dependencies, [('otherapp', '0001_initial')]) + def test_trim_apps(self): """ Trim does not remove dependencies but does remove unwanted apps. @@ -2524,6 +2544,14 @@ class MigrationSuggestNameTests(SimpleTestCase): suggest_name = migration.suggest_name() self.assertIs(suggest_name.startswith('auto_'), True) + def test_none_name_with_initial_true(self): + class Migration(migrations.Migration): + initial = True + operations = [migrations.RunSQL('SELECT 1 FROM person;')] + + migration = Migration('0001_initial', 'test_app') + self.assertEqual(migration.suggest_name(), 'initial') + def test_auto(self): migration = migrations.Migration('0001_initial', 'test_app') suggest_name = migration.suggest_name()