DB Router default issue

75 views
Skip to first unread message

JJ Zolper

unread,
Jul 10, 2020, 12:03:46 PM7/10/20
to Django users
Hello, I am trying to prevent models in my api app from migrating into the default database but I'm confused about the router. It seems every time I run migrate even with the router it continues to migrate.


#apps.py
from django.apps import AppConfig

class ApiConfig(AppConfig):
name = 'api'
label = 'api'


#settings.py
DATABASE_ROUTERS = ['myproject.dev_db_router.APIRouter',]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'default.sqlite3'),
},
'mydb': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'mydb.sqlite3'),
},
}


#dev_db_router.py
class APIRouter:
"""
A router to control all database operations on models in the
api application.
"""
route_app_labels = {'api',}

def db_for_read(self, model, **hints):
"""
Attempts to read api models go to mydb.
"""
if model._meta.app_label in self.route_app_labels:
return 'mydb'
return False

def db_for_write(self, model, **hints):
"""
Attempts to write api models goes to mydb.
"""
if model._meta.app_label in self.route_app_labels:
return 'mydb'
return False

def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the api app only appears in the
'mydb' database.
"""
if app_label in self.route_app_labels:
return db == 'mydb'
return False

I've tried:

python manage.py migrate --database=default

python manage.py migrate


etc and every time it says:

  Applying api.0001_initial... OK


Even though I told it False if it does not meet the case of db == 'mydb'. I can specify 'mydb' and it says it works:

python manage.py migrate --database=mydb

but my concern is it always migrates into default even when I'm trying to tell it not to. In the future there will be models I do want to migrate into default, but not these in the api app. Based on the documentation I'm doing everything correctly.

What am I not understanding?

Thank you.

Best,

JJ

JJ Zolper

unread,
Jul 14, 2020, 12:35:17 PM7/14/20
to Django users
Has anyone experienced this same thing? Anyone have a lot of experience working with multiple databases using a router? It's just a little unclear how I'm supposed to better control it.

Roger Gammans

unread,
Jul 14, 2020, 12:55:34 PM7/14/20
to django...@googlegroups.com
In allow_migrate () where you have 'return False', I've ended up (in my otherwise very similar router) with:

return db != 'mydb'

You certainly don't want return False as that will disable all migrations outside of your app_label, you want to return True,
only in the cases when the db is not your one; so everything else flows normally.  But globally returnning False disables the migrations in the normal case as you have found.

There are some awkward corner cases ; around mutli databases; some of which are probably a bug - (for instance - all migrations whether relevant or not are recorded in both databases) But I don't think that's your problem here.
allow_migrate(
self
, db, app_label, 
model_name=
None
, 
**hints

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1c50646c-6fbe-4d47-84f1-1250e4c515a1o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages