#23135: Multiple routers cause problems with migration/sql management commands
--------------------------------------------+----------------------
Reporter: Geladriet | Owner: nobody
Type: Bug | Status: new
Component: Core (Management commands) | Version: 1.7-rc-2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------------+----------------------
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
--
Ticket URL: <https://code.djangoproject.com/ticket/23135>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.c2259bdc33ea5516f0b56b99d7238970%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.