3

My code has access to 3 databases. syncdb must create all of the model tables in two of the databases. I am unsure how to do this. The below does not work.

DATABASES = {
    'default': {}, # empty. default is required
    'db_1': { # want tables created in this
       ...
    },
    'db_2': { # want tables created in this
       ...
    },
    'other': { # do NOT want tables created in this
       ...
    },   
}

router: (a separate router handles auth tables)

import random
class OtherRouter(object):
    def db_for_read(self, model, **hints):
        return random.choice(['db_1', 'db_2'])

    def db_for_write(self, model, **hints):
        return "db_1"

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_syncdb(self, db, model):
        db_list = ('db_1', 'db_2')
        if db in db_list:
            return True
        return None

1 Answer 1

3

Got it. Syncdb has to be run with the db parameter. Also, maybe (not sure it matters) allow_syncdb should return False, not None, in other cases.

syncdb --database='db_1'
syncdb --database='db_1'
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.