﻿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
23135	Multiple routers cause problems with migration/sql management commands	Geladriet	nobody	"Hello

I'm currently testing django 1.7 as much as I can with our multiple database configuration and found an bug.

If you are using multiple routers, there seems a problem to get the right target database.

Sample:

settings.py
{{{DATABASE_ROUTERS = ['SampleModel.router.SampleModelRouter', 'SampleModel2.router.SampleModelRouter2']}}}

Both models and routers are the same but have another name.

router.py
{{{
class SampleModelRouter2(object):
    """"""
    A router to control all database operations on models.
    """"""

    def db_for_read(self, model, **hints):
        """"""
        Attempts to read go to game db.
        """"""
        if model._meta.app_label == 'SampleModel2':
            return 'second_db'
        return None

    def db_for_write(self, model, **hints):
        """"""
        Attempts to write go to game db.
        """"""
        if model._meta.app_label == 'SampleModel2':
            return 'second_db'
        return None

    def allow_migrate(self, db, model):
        """"""
        Make sure the app only appears on the right db.
        """"""
        if db == 'second_db':
            return model._meta.app_label == 'SampleModel2'
        elif model._meta.app_label == 'SampleModel2':
            return False
        return None
}}}

Output model 1 sqlall:

{{{
[mgysin@localhost Demo]$ python manage.py sqlall SampleModel --database=second_db
BEGIN;
CREATE TABLE ""SampleModel_location"" (
    ""id"" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    ""name"" varchar(50) NOT NULL
)
;

COMMIT;
}}}

Now the same for model 2:
{{{
[mgysin@localhost Demo]$ python manage.py sqlall SampleModel2 --database=second_db
[mgysin@localhost Demo]$ 
}}}

If I change the order of the routers, model 2 starts working:

settings.py
{{{DATABASE_ROUTERS = ['SampleModel2.router.SampleModelRouter2', 'SampleModel.router.SampleModelRouter']}}}

sqlall:
{{{
[mgysin@localhost Demo]$ python manage.py sqlall SampleModel2 --database=second_db
BEGIN;
CREATE TABLE ""SampleModel2_location"" (
    ""id"" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    ""name"" varchar(50) NOT NULL
)
;

COMMIT;
}}}

Attached is the sample project with the problem.
Please note this bug is also in 1.7-rc-1

Greets
Manuel"	Bug	closed	Core (Management commands)	1.7-rc-2	Normal	needsinfo			Unreviewed	0	0	0	0	0	0
