-1

I am new to Django, your help would be greatly appreciated. please let me know what mistakes i am making. can someone give me an example with at least 2 apps and 2 Databases.

I have 3 apps cherry, apple and mango in my Django project. For every app there is "models_cherry.py, models_apple.py and models_mango.py". I have created 3 databases in MySQL workbench DB1, DB2 and DB3.

When i fires following queries on windows PowerShell for migrations, it should create tables in the databases. migrations should be done on databases.

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

The above commands create tables for only one model for one database.

My Question is, i want to create tables for all the models for all respective databases. i.e. For classes in file models_cherry.py into database DB1, for classes in file models_apple.py into DB2 and for classes in models_mango.py into DB3?

Here is a code for settings.py and routers files:

#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
IMSoP
  • 89,526
  • 13
  • 117
  • 169
SSS
  • 73
  • 11

1 Answers1

0

Some questions before answering your question-

  1. Why have you created 3 databases, you'll only be needing one. You will need multiple tables in your case.
  2. Why have you named databases as DBTable1.2.. ?

Answering your question, since in your "settings.py" you mentioned "Database" as "DBTable1", it will only create tables in your "DBTable1" database.

One database can have multiple tables. So name your database accordingly.

  • thanks for your reply. Asnwer to your questions : 1) I want to setup connection of My Django project apps with multiple databases,thats why i have created multiple databases in MySQL workbench names are like DBTable1,DBTable2 etc. In settings.py we normally use for database connection right? i am only able to create tables in default database that is DBTable1, but i am facing problem with connection between other databases. Can you help me with that? – SSS Aug 19 '20 at 10:42
  • The basic thing that you are missing is that you can only connect one "Database" with one application. Multiple databases make no sense in your case. You need multiple tables, where each table will store the info of each model. In settings.py, we connect application to a **Single "Database"** and that is the reason it is creating all the tables in only "DBTable1". Secondly, You shouldn't name the database as DBTable as table is a component of database not database itself! – Atharva Kango Aug 19 '20 at 10:48
  • @ Atharva Kango - yes i know we can connect only one database with one application, but i have multiple application (cherry, apple and mango)and want to connect with multiple databases, that why i have created DBTable1,DBTable2, DBTable3. and DBTable1 is just name of database which i have mentioned only on stack overflow not in my actual database. Because i dont wan to mentioned original names of database. – SSS Aug 19 '20 at 11:08