One Django App/Model to Multiple databases

Viewed 74

I'm on a Django project & for performance and RoadMaping reasons I will need to use a Multi-BDD system.

However I am a little stuck let me explain myself, I created the databases, I added a Routing URL'S to specify which database to use and when to use it. I have a single 'Application in this project' and also a single & unique 'Database model', this last (model) logically must be duplicated in all my databases during my migration. THE QUESTION:

Why when I migrate this model it is only migrated in a single database and not in the other while the migration table is duplicated, I cannot understand the why and how I will appreciate your feedback. Thank you in advance !

Here is my settings.py

# Including le DB_Router
DATABASE_ROUTERS = ['routers.db_routers.AuthRouter',
                    'routers.db_routers.VIGRouter',
                    'routers.db_routers.VISRouter',
                    'routers.db_routers.DURRouter',
                    'routers.db_routers.IPCRouter',
                    ]

DATABASES = {
    'default': {},
    'izlog': {
        'ENGINE': ENGINE,
        'NAME': 'izlog',
        'USER': USER,
        'PASSWORD': PASSWORD,
        'HOST': HOST,
        'PORT': PORT,
    },
    'db_vig': {
        'ENGINE': ENGINE,
        'NAME': DBSNAME['db_VIG'],
        'USER': USER,
        'PASSWORD': PASSWORD,
        'HOST': HOST,
        'PORT': PORT,
    },
    'db_vis': {
        'ENGINE': ENGINE,
        'NAME': DBSNAME['db_VIS'],
        'USER': USER,
        'PASSWORD': PASSWORD,
        'HOST': HOST,
        'PORT': PORT,
    },
Here is my db_routers.py

class VIGRouter:
    route_app_labels = {'izLogApp'}

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

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

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


class VISRouter:
    route_app_labels = {'izLogApp'}

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

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

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

and here is my model

class log_weblinks(models.Model):
    id_societe = models.CharField(max_length=32, null=True)
    code_client = models.CharField(max_length=32, null=True)
    date = models.DateTimeField(null=True)
    log = models.CharField(max_length=32, null=True)

    def __str__(self):
        return self.id_societe

    def readableDateCommande(self):
        date_time_obj = datetime.datetime.strptime(self.date, '%Y%m%d')
        return date_time_obj.strftime('%Y-%m-%d HH:MM:SS')


class connexion_log_weblinks(models.Model):
    id_societe = models.CharField(max_length=32, null=True)
    code_client = models.CharField(max_length=32, null=True)
    date = models.DateTimeField(null=True)
    log = models.CharField(max_length=32, null=True)

    def __str__(self):
        return self.log

    def readableDateCommande(self):
        date_time_obj = datetime.datetime.strptime(self.date, '%Y%m%d')
        return date_time_obj.strftime('%Y-%m-%d HH:MM:SS')

1 Answers

I guess that you use python manage.py migrate to apply your migrations. This command will only work for the default database. When running a multi database app, you need to specifiy target database with --database option.

For example : python manage.py migrate --database db_vig

Related