首页 > 解决方案 > 如何在 Django 中迁移多个数据库?

问题描述

我正在用 Django 做一个项目,数据太多了,所以我需要形成不同的数据库来存储它们。我为每个应用程序制作了一个数据库路由器以匹配唯一的数据库,但是当我迁移它们时,所有的表都迁移到了同一个数据库,这里是代码:

from django.conf import settings

DATABASE_MAPPING = settings.DATABASES_APPS_MAPPING

class DatebaseAppsRouter(object):
    """
    该类为连接不同数据库的路由,在setting中通过设置DATABASE_MAPPING来匹配数据库
    例:
    DATABASE_APPS_MAAPING = {"app1":"db1", "app2":"db2"}
    """
    def db_for_read(self, model, **kwargs):
        """将所有读操作指向指定的数据库"""
        if model._meta.app_label in DATABASE_MAPPING:
            return DATABASE_MAPPING[model._meta.app_label]
        return None

    def db_for_write(self, model, **kwargs):
        """将所有写操作指向指定的数据库"""
        if model._meta.app_label in DATABASE_MAPPING:
            return DATABASE_MAPPING[model._meta.app_label]
        return None


    def allow_relation(self, obj1, obj2, **kwargs):
        """允许使用相同数据库的应用程序之间的任何关系"""
        db_obj1 = DATABASE_MAPPING.get(obj1._meta.app_label)
        db_obj2 = DATABASE_MAPPING.get(obj2._meta.app_label)
        if db_obj1 and db_obj2:
            if db_obj1 == db_obj2:
                return True
            else:
                return False
        else:
            return None


    def allow_syncdb(self, db, model):
        """确保这些应用程序只出现在相关的数据库中"""
        if db in DATABASE_MAPPING.values():
            return DATABASE_MAPPING.get(model._meta.app_label) == db
        elif model._meta.app_label in DATABASE_MAPPING:
            return False
        return None


    def allow_migrate(self, db, app_label, model=None, **kwargs):
        """确保身份验证应用程序只出现在login数据库中"""
        if db in DATABASE_MAPPING.values():
            return DATABASE_MAPPING.get(app_label) == db
        elif app_label in DATABASE_MAPPING:
            return False
        return None

通过使用路由器,我想迁移多个数据库,但它不起作用

System check identified some issues:

WARNINGS:
?: (urls.W001) Your URL pattern '^login/$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.
?: (urls.W001) Your URL pattern '^main/$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.
?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
Operations to perform:
  Apply all migrations: Themepark, admin, auth, contenttypes, ginseng_holding_hotel, holiday_resort, login, nature_scenic_area, sessions, the_scent_spot_trusteeship, tourist_communications, travel_agency
Running migrations:
  No migrations to apply.

那么如何解决这个问题呢?谢谢!

标签: pythondjango

解决方案


我认为问题出在你的 urls.py

如果您正在使用django.urls.path(),则不必添加胡萝卜符号和美元符号并这样做。

urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', views.login, name='login'),
    path('main/', views.main, name='main'),
]

但如果你正在使用,django.urls.url()那么你应该这样做

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^login/', views.login, name='login'),
    url(r'^main/', views.main, name='main'),
]

如果你不介意,你能告诉我们你的 urls.py 吗?


推荐阅读