I'm in the process of configuring my Django app for multiple databases, and as a stop-gap measure, I'm using the following configuration in my settings.py:
READ_DATABASE = {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'thedatabase',
'USER': 'read_only',
'PASSWORD': '',
'OPTIONS': {
'init_command': 'SET storage_engine=INNODB'
}
}
WRITE_DATABASE = {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'thedatabase',
'USER': 'root',
'PASSWORD': '',
'OPTIONS': {
'init_command': 'SET storage_engine=INNODB'
}
}
DATABASES = {'default': WRITE_DATABASE,
'read': READ_DATABASE}
These point to the same database, in order to emulate a master-slave pair. The read_only
user, as you might guess, only has read permissions.
My database router looks like this:
from django.conf import settings
class MasterSlaveRouter(object):
def db_for_read(self, model, **hints):
return 'read'
def db_for_write(self, model, **hints):
return 'default'
def allow_relation(self, db1, db2, **hints):
return True
def allow_syncdb(self, db, model):
return db in ('default',)
When running syncdb, it now crashes immediately after setting up the superuser, with the error:
django.db.utils.IntegrityError: (1062, "Duplicate entry 'auth-permission' for key 'app_label'")
The offending SQL that's failing to execute is:
INSERT INTO `django_content_type` (`name`, `app_label`, `model`) VALUES (permission, auth, permission)
This error only occurs when the second read database is sepcified in my settings.py
, when only a default
database is present, the syncdb command completes successfully.
Any suggestions what could be causing this?
Edit: Django version is 1.2.3
DATABASE_ROUTERS
? It looks likepost_syncdb
has been triggered twice. – okm May 8 '12 at 14:18DATABASE_ROUTERS = ['path.to.MasterSlaveRouter']
is in my config file. – majackson May 8 '12 at 15:28path.to
;and try to putMasterSlaveRouter
inside settings and then setDATABASE_ROUTERS = ['settings.MasterSlaveRouter']
? – okm May 8 '12 at 15:35import pdb; pdb.set_trace
to thedb_for_read/write
methods verify this. The breakpoints are being hit and the discoverability of the router is definitely not the problem. Thanks for the suggestion though. – majackson May 8 '12 at 15:57post_syncdb
be bound more than once thus contenttypes wants to generate content_type for Permission model twice times, thus the error – okm May 8 '12 at 16:03