Multiple databases in Django 1.7

65 views
Skip to first unread message

Łukasz Pauszek

unread,
Dec 9, 2015, 5:47:42 AM12/9/15
to Django users
Hello,
I'm trying to configure project in Django 1.7 to use multiple databases, for now I'm following official documentation to configure 2 dbs.
Problem is that when I run: ./manage.py makemigrations myapp only changes in default database are detected. And ./manage.py migrate --database=main_db doesn't see model which should be created in it.
What should I do to update second database?
Below I'm pasting code which I think may be relevant:

project/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'default.sqlite3'),
    },
    'main_db': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'main_db.sqlite3')
    }
}

DATABASE_ROUTERS = ['myapp.routers.SelectDatabaseRouter']

myapp/models.py
class TestingDefaultDatabase(models.Model):

    class Meta:
        verbose_name = "TestingDefaultDatabase"
        verbose_name_plural = "TestingDefaultDatabases"

        name = models.CharField(max_length=50)
        surname = models.CharField(max_length=50)


class TestingMainDatabase(models.Model):

    class Meta:
        verbose_name = "TestingMainDatabase"
        verbose_name_plural = "TestingMainDatabases"
        app_label = 'main'

    name = models.CharField(max_length=50)
    surname = models.CharField(max_length=50)

myapp/routers.py
class SelectDatabaseRouter(object):
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'main':
            return 'main_db'
        return None

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

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label == 'main' or \
                obj2._meta.app_label == 'main':
            return True
        return None

    def allow_migrate(self, db, model):
        if db == 'main_db':
            return model._meta.app_label == 'main'
        elif model._meta.app_label == 'main':
            return False
        return None

Am I missing or incorrectly configured something? Or perhaps I'm not using appropriate commands?
Thank you for help.
ŁP

learn django

unread,
Dec 9, 2015, 4:35:58 PM12/9/15
to Django users
Hi LP.

Your allow_migrate function differs in the format from what is mentioned in the link below.

I have tried this last week and it works fine.

Łukasz Pauszek

unread,
Dec 9, 2015, 5:46:22 PM12/9/15
to Django users
Hey,
thank you for fast response.
Yes, I'm aware that allow_migrate format from the one mentioned in link you provided because (as is mentioned in title) I'm using 
Django 1.7 and the code I provided is almost copy/paste of what you can find under the same link but for v. 1.7
Things I changed in code are data base name and _meta.app_label
Reply all
Reply to author
Forward
0 new messages