Django多个数据库错误与路由 [英] Django multiple databases error with routes
本文介绍了Django多个数据库错误与路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在项目中使用了3个数据库:
I'm using 3 databases in my project:
DATABASES = {
'default': {
(postgres, read, write)...
},
'admission_db': {
(postgres, read, write)...
},
'journals_db': {
(mysql, read only)...
}
}
DATABASE_ROUTERS = [
'main.lib.DbRouter.DbRouter',
]
写数据库路由器:
class DbRouter:
...
def allow_migrate(self, db, app_label, model=None, **hints):
if db == 'admission_db':
if model and model._meta.app_label == 'admission':
return True
return app_label == 'admission'
elif db == 'journals_db':
return False
return None
当我执行迁移命令时
python manage.py migrate admission --database admission_db
它可以正常迁移,但是当我尝试制作时
it migrates normally, but when i try to make
python manage.py migrate,
它抛出
django.db.utils.OperationalError: no such table: admission_educationform
因此,我可以在其他应用程序上进行迁移.(python 3.5.2,django 1.10.5)可能是什么原因引起的?
because of it i can make make migrate on other apps. (python 3.5.2, django 1.10.5) what can be cause of problem?
推荐答案
添加了对用于magration的数据库的检查:
Added check of db used in magration:
def allow_migrate(self, db, app_label, model=None, **hints):
if db == 'default':
if app_label == 'admission':
return False
elif model and model._meta.app_label == 'admission':
return False
if db == 'admission_db':
if model and model._meta.app_label == 'admission':
return True
return app_label == 'admission'
elif db == 'journals_db':
return False
return None
因此,我应该启动2个迁移命令:
So, i should launch 2 migrate commands:
python migrate admission
python migrate
现在可以正常工作了.
这篇关于Django多个数据库错误与路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文