我正在创建一个名为“db_manager”的Django项目,并希望创建两个数据库:一个用于db_manager模型,另一个用于核心Django模型(auth、admin、contentTypes、session等)。
首先,我运行迁移以在adw_core数据库中创建核心模型。然后,我运行迁移以在adw_facts数据库中创建db_manager模型。我使用以下命令:
settings.py
当我运行迁移以在adw_facts数据库中创建db_manager模型时,会出现以下错误:
运行时错误: 创建新内容类型时出错。请先确保migrate了contenttypes,再尝试单独迁移应用程序。
我确认表'core_db.django_content_type'存在。问题一定是django.contrib.contenttypes应用程序正在尝试为adw_facts db中的db_manager创建contentTypes,但表'core_db.django_content_type'位于adw_core db中。
我不想删除contenttypes应用程序,因为我正在将其与其他Django功能一起使用。
我可以考虑以下解决方案:
1. 为facts模型禁用contenttypes 2. 为facts_db创建第二个contenttypes表
这两种方法都不是很理想。如果您有更好的解决方案,请告诉我。
首先,我运行迁移以在adw_core数据库中创建核心模型。然后,我运行迁移以在adw_facts数据库中创建db_manager模型。我使用以下命令:
python manage.py migrate db_manager --database adw_facts
我设置了路由器,将应用程序指向适当的数据库:settings.py
DATABASE_ROUTERS = ['data_warehouse.db_routers.DatabaseRouter']
MAP_APPS_TO_DB = {
'db_manager': 'adw_facts',
'django.contrib.auth': 'adw_core',
'django.contrib.sessions': 'adw_core',
}
db_routers.py
class DatabaseRouter(object):
def db_for_read(self, model, **hints):
if model._meta.app_label in settings.MAP_APPS_TO_DB:
return settings.MAP_APPS_TO_DB[model._meta.app_label]
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in settings.MAP_APPS_TO_DB:
return settings.MAP_APPS_TO_DB[model._meta.app_label]
return None
def allow_relation(self, obj1, obj2, **hints):
# Allow any relation between apps that use the same database.
db_obj1 = settings.MAP_APPS_TO_DB.get(obj1._meta.app_label)
db_obj2 = settings.MAP_APPS_TO_DB.get(obj2._meta.app_label)
if db_obj1 and db_obj2:
if db_obj1 == db_obj2:
return True
else:
return False
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in settings.MAP_APPS_TO_DB:
return db == settings.MAP_APPS_TO_DB[app_label]
return None
当我运行迁移以在adw_facts数据库中创建db_manager模型时,会出现以下错误:
运行时错误: 创建新内容类型时出错。请先确保migrate了contenttypes,再尝试单独迁移应用程序。
我确认表'core_db.django_content_type'存在。问题一定是django.contrib.contenttypes应用程序正在尝试为adw_facts db中的db_manager创建contentTypes,但表'core_db.django_content_type'位于adw_core db中。
我不想删除contenttypes应用程序,因为我正在将其与其他Django功能一起使用。
我可以考虑以下解决方案:
1. 为facts模型禁用contenttypes 2. 为facts_db创建第二个contenttypes表
这两种方法都不是很理想。如果您有更好的解决方案,请告诉我。