0

So I am working on a project where every application has its own database. However, when I sync the database, only the default database is synced. I need help to see the issue.

# from settings.py
try:
    from local_settings import *
    INSTALLED_APPS += LOCAL_APPS
    for app in LOCAL_APPS:
        _appdb = DATABASES['default'].copy()
        _appdb['NAME'] = app
        DATABASES[app] = _appdb
except ImportError:
    pass

I have already verified that each local app has a corresponding database generated dynamically. Here is the db router

import os

MY_LOCAL_APPS = []
with open(os.path.join(os.path.dirname(__file__), "apps.csv")) as appsfile:
    MY_LOCAL_APPS = appsfile.readlines()

MY_LOCAL_APPS = [x.strip() for x in MY_LOCAL_APPS]

class JauntRouter(object):
    def db_for_read(self, model, **hints):
        """
        Attempts to read app models go to app_db.
        """
        if model._meta.app_label in MY_LOCAL_APPS: 
            return model._meta.app_label
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write app models go to app_db.
        """
        if model._meta.app_label in MY_LOCAL_APPS: 
            return model._meta.app_label
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if model._meta.app_label in MY_LOCAL_APPS: 
            return model._meta.app_label
        return None

    def allow_syncdb(self, db, model):
        if db in MY_LOCAL_APPS:
            return model._meta.app_label == db
        return None

When I run syncdb however, tables are created in the default database. Please note that I have printed out the INSTALLED_APPS and DATABASES from the settings file and they appear correct so I don't think that is the issue.

Help will be greatly appreciated.

1 Answer 1

1

I think you should use the --database switch of syncdb.

https://docs.djangoproject.com/en/1.6/topics/db/multi-db/#synchronizing-your-databases

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.