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