﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26117	Add database routers support to initial migration detection	Scott Sexton	Scott Sexton	"In the case where you have an app that defines a routers.py to associate certain models with a different database, the initial migration for that app cannot be faked with --fake-initial. Because some of the models exist in a different database than the one Django is currently migrating, it will ignore the fake and try to apply the migration anyway, giving you an OperationalError that one of the tables already exists.

The documentation reveals the location of the bug: ""For an initial migration that creates one or more tables (CreateModel operation), Django checks that all of those tables already exist in the database and fake-applies the migration if so."" But does it check the right database for those tables? No.

The bug occurs occurs in django.db.migrations.executor.py in the detect_soft_applied method. It simply compares all models it finds to a list of tables for the current db `existing_table_names = self.connection.introspection.table_names(self.connection.cursor())`, however, if a certain model exists in a different DB as defined in the app's routers.py, this check will always fail. 

Instead, this method should probably consider the return value of the allow_migrate() method in the app's routers.py before trying to confirm whether that model should exist in the current db. "	Bug	closed	Migrations	1.9	Normal	fixed			Accepted	1	0	0	1	0	0
