I'm facing a problem when using multiple databases in Django.
Here's the scenario:
I have a django project, which is divided into two applications: app1
and app2
. App1
will take care of authentication and custom modules (ie. it has its own models.py
) and App2
is normal web application (ie. it has its own models.py
with models in it).
Settings.py looks like this:
DATABASE_ROUTERS = ['app1.router.AuthRouter', 'app1.router.App1Router', 'app2.router.App2Router']
DATABASES = {
'default': {
[...]
},
'app2': {
[...]
}
}
As you can see, I have two databases (both PSQL 9.2), default
(used for app1
application) and app2
(used for app2
application) and I've defined such 3 routers (1. for authentification, 2. for app1 model
and last for app2 model
.
Code for app1.router.AuthRouter
and app1.router.App1Router
is following:
class AuthRouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label == 'auth':
return 'default'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'auth':
return 'default'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'auth' or obj2._meta.app_label == 'auth':
return True
return None
def allow_syncdb(self, db, model):
if db == 'default':
return model._meta.app_label == 'auth'
elif model._meta.app_label == 'auth':
return False
return None
class App1Router(object):
def db_for_read(self, model, **hints):
if model._meta.app_label == 'app1':
return 'default'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'app1':
return 'default'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'app1' or obj2._meta.app_label == 'app1':
return True
return None
def allow_syncdb(self, db, model):
if db == 'default':
return model._meta.app_label == 'app1'
elif model._meta.app_label == 'app1':
return False
return None
Problem is that, when I do syncdb
, it does correctly create auth_
tables, but it does not create django_
tables, which fails in error.
[marek@t420 multipledb_test]$ python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
[more errors coming]
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
YOU SEE? THERE ARE NO DJANGO TABLES CREATED - no django_admin_log
, no django_content_type
, no django_session
!!!
Any hints? It's driving me crazy for three days!