EviSvil
EviSvil

Reputation: 636

Django Multiple Database - Duplicate tables in default

I have a Django Application with two database. Even with routers, subapp tables are written in default DB. SubApp DB has only its own table.

In core.models Im not define the appl_label In subapp models.py for every class, I define the app_label

SubApp models.py

class MyModel ...
  ...
  class Meta:
     app_label               = 'subapp_name'
     db_table                = 'my_model'

In default settings I have defined the routing:

DATABASE_ROUTERS = ["core.routers.DefaultRouter" , "subapp.routers.SubAppRouter"]

In subapp router in allow_migrate, I have this:

def allow_migrate(self, db, app_label, model_name = None, **hints):
        if app_label == 'subapp_name' and db == 'subapp_name':
            return True
        
        return None

This works well. In SubApp Database I only have MyModel table and a DjangoMigration table (this one has all migrations rows, even from default modules).

This is the allow_migrate in DefaultRouter:

def allow_migrate(self, db, app_label, model_name = None, **hints):
        route_app_labels = { "admin", "auth", "contenttypes", "core", "sessions" }
        if app_label in self.route_app_labels:
            return db == "default"
        
        return None

Unfortunally, subapp MyModel is create in default database too but subapp is not inside route_app_labels.

Why is this happening?

Upvotes: 0

Views: 79

Answers (1)

Harshit verma
Harshit verma

Reputation: 548

Try this:

def allow_migrate(self, db, app_label, model_name=None, **hints):
    if app_label == 'subapp_name':
        return db == 'subapp_name'
    return None

Upvotes: 1

Related Questions