【问题标题】:How to do connection or migrations between different MySQL databases and Different Django App's?如何在不同的 MySQL 数据库和不同的 Django App 之间进行连接或迁移?
【发布时间】:2020-12-08 12:12:44
【问题描述】:

我是 Django 新手,非常感谢您的帮助。请让我知道我犯了什么错误。谁能给我一个至少有 2 个应用程序和 2 个数据库的例子。

我的 Django 项目中有 3 个应用程序樱桃、苹果和芒果。每个应用程序都有“models_cherry.py、models_apple.py 和 models_mango.py”。我在 MySQL 工作台 DB1、DB2 和 DB3 中创建了 3 个数据库。

当我在 Windows PowerShell 上触发以下查询以进行迁移时,它应该在数据库中创建表。迁移应该在数据库上完成。

  1. python manage.py makemigrations 2) python manage.py migrate

以上命令只为一个数据库的一个模型创建表。

我的问题是,我想为所有相应数据库的所有模型创建表。 即,对于文件 models_cherry.py 中的类到数据库 DB1,对于文件 models_apple.py 中的类到 DB2 和对于 models_mango.py 中的类到 DB3?

下面是 settings.py 和 routers 文件的代码:

#Settings.py

DATABASE_ROUTERS =['apple.Routers.core_router.core_router', #Path to router file
                    'mango.Routers.man_router.man_router']
DATABASE_APPS_MAPPING = {'core':'core',
                          'man':'man'}

DATABASES = {
        'default': {
                'ENGINE': 'django.db.backends.mysql',
                'HOST': 'localhost',
                'PORT': '3306',
                'NAME': 'DB1',
                'USER': 'root',
                'PASSWORD': 'root',
                'OPTIONS': {
                    'autocommit': True,
                },
            },
    'core': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': 'localhost',
        'PORT': '3306',
        'NAME': ' DB2',
        'USER': 'root',
        'PASSWORD': 'root',
        'OPTIONS': {
            'autocommit': True,
        },
    },
    'man': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': 'localhost',
            'PORT': '3306',
            'NAME': ' DB3',
            'USER': 'root',
            'PASSWORD': 'root',
            'OPTIONS': {
                'autocommit': True,
            },
        },

}

# man_router.py
class man_router:
    route_app_labels = {'man'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'man'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'man'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if (
            obj1._meta.app_label in self.route_app_labels or
            obj2._meta.app_label in self.route_app_labels
        ):
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'man'
        return None
# core_router.py
class core_router:
    route_app_labels = {'core'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'core'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'core'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if (
            obj1._meta.app_label in self.route_app_labels or
            obj2._meta.app_label in self.route_app_labels
        ):
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'core'
        return None

【问题讨论】:

    标签: python-3.x django django-models django-migrations


    【解决方案1】:

    回答你的问题之前的一些问题-

    1. 为什么您创建了 3 个数据库,您只需要一个。您将需要多个 适合您的表格。
    2. 为什么将数据库命名为 DBTable1.2..?

    回答您的问题,因为在您的“settings.py”中您提到“数据库”为“DBTable1”,它只会在您的“DBTable1”数据库中创建表。

    一个数据库可以有多个表。因此,相应地命名您的数据库。

    【讨论】:

    • 感谢您的回复。回答您的问题:1)我想设置我的 Django 项目应用程序与多个数据库的连接,这就是为什么我在 MySQL 工作台名称中创建了多个数据库,例如 DBTable1、DBTable2 等。在 settings.py 中,我们通常用于数据库连接权?我只能在默认数据库 DBTable1 中创建表,但我面临其他数据库之间连接的问题。你能帮我解决这个问题吗?
    • 您缺少的基本内容是您只能将一个“数据库”与一个应用程序连接。在您的情况下,多个数据库没有意义。您需要多个表,每个表将存储每个模型的信息。在 settings.py 中,我们将应用程序连接到 单个“数据库”,这就是它仅在“DBTable1”中创建所有表的原因。其次,您不应该将数据库命名为 DBTable,因为表是数据库的组成部分,而不是数据库本身!
    • @ Atharva Kango - 是的,我知道我们只能将一个数据库与一个应用程序连接,但我有多个应用程序(樱桃、苹果和芒果)并且想要连接多个数据库,这就是我创建的原因数据库表 1、数据库表 2、数据库表 3。 DBTable1 只是我在堆栈溢出时提到的数据库的名称,而不是在我的实际数据库中。因为我不想提及数据库的原始名称。
    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2021-09-11
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多