Multiples data bases with django

1

Hello, I am trying to use several databases I have followed the documentation and several examples that I have seen but the models dissected by the router that I created are not created in any database.

    class dbRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
    """
    Attempts to read auth models go to auth_db.
    """
    if model._meta.app_label == 'compra':
        return 'compra_database'
    return None

def db_for_write(self, model, **hints):
    """
    Attempts to write auth models go to auth_db.
    """
    if model._meta.app_label == 'compra':
        return 'compra_database'
    return None

def allow_relation(self, obj1, obj2, **hints):
    """
    Allow relations if a model in the auth app is involved.
    """
    if obj1._meta.app_label == 'compra' or \
       obj2._meta.app_label == 'compra':
       return True        
    return True

def allow_migrate(self, db, app_label, model_name=None, **hints):
    """
    Make sure the auth app only appears in the 'auth_db'
    database.
    """
    if app_label == 'compra':
        return db == 'compra_database'
    return None

and these are the settings in the settings.

file
    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql', 
    'NAME': 'Django',
    'USER': 'root',
    'PASSWORD': 'R4spb3rryPi_8d',
     },
     'auth_database': {
    'ENGINE': 'django.db.backends.mysql', 
    'NAME': 'Django',
    'USER': 'XXXXXXXXX',
    'PASSWORD': 'XXXXXXXXXXX',
       },
    'compra_database': {
    'ENGINE': 'django.db.backends.mysql', 
    'NAME': 'Compras',
    'USER': 'XXXXXXXXX',
    'PASSWORD': 'XXXXXXXXXXX',
     },
     'crohn_database': {
    'ENGINE': 'django.db.backends.mysql', 
    'NAME': 'Crohn',
    'USER': 'XXXXXXXXX',
    'PASSWORD': 'XXXXXXXXXXX',
    },
    }

    DATABASE_ROUTERS = ['raspi.dbrouter.dbRouter']

the makemigrations commands create migration tables in all the databases and migrate does not throw any errors but nevertheless only the auth, session and contrib tables that appear in the Default database appear.

a greeting and thanks.

    
asked by Juan Antonoio Contreras 01.09.2018 в 21:51
source

1 answer

0

The databases are not created automatically, you have to create them manually. Once you have created the BDs, the migrations run and your tables should be created in their respective BDs

    
answered by 04.09.2018 в 09:05