2

I have been trying to setup my project on Django1.3 to use multiple databases, in my case different sqlite3 files. I have been reading the Django documentation as well a lot of googling but in vain.

Problems faced

  1. When using the syncdb with --database, why the tables for that particular application are not getting created in either default database or the other db?
  2. Django framework internal tables like auth are getting created for all the databases mentioned. Is this the way the feature is supposed to behave OR I have wrongly configured the same?

My Code

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(SITE_ROOT, "db/defaultdb.sqlite"),
    },
    'app1': {
             'ENGINE': 'django.db.backends.sqlite3',
             'NAME': os.path.join(SITE_ROOT, "db/app1db.sqlite"),
    }
}

DATABASE_ROUTERS = ['dbrouter.MyAppRouter']

dbrouter.py

class MyAppRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'app1':
            return 'app1db'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'app1':
            return 'app1db'
        return None

    def allow_syncdb(self, db, model):
        if db == 'app1db':
            return model._meta.app_label == 'app1'
        elif model._meta.app_label == 'app1':
            return False
        return None

Thanks everyone for your time.

1 Answer 1

2

In your db_for_read and db_for_write methods,

if model._meta.app_label == 'app1':
    return 'app1db'
return None

You must return the db alias, which is app1 according to your settings.DATABASES. There is a similar issue in your allow_syncdb method. Have you read the docs?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.