Answer the question
In order to leave comments, you need to log in
How to set up 2 DBs in Django?
The project uses 3 databases and a database router:
DATABASES = {
'default': {
(postgres, читать, писать)...
},
'admission_db': {
(postgres, читать, писать)...
},
'journals_db': {
(mysql, только читать)...
}
}
DATABASE_ROUTERS = [
'main.lib.DbRouter.DbRouter',
]
class DbRouter:
...
def allow_migrate(self, db, app_label, model=None, **hints):
if db == 'admission_db':
if model and model._meta.app_label == 'admission':
return True
return app_label == 'admission'
elif db == 'journals_db':
return False
return None
$ python manage.py migrate admission --database admission_db
$ python manage.py migrate
django.db.utils.OperationalError: no such table: admission_educationform
def allow_migrate(self, db, app_label, model=None, **hints):
if db == 'default':
if app_label == 'admission':
return False
elif model and model._meta.app_label == 'admission':
return False
if db == 'admission_db':
if model and model._meta.app_label == 'admission':
return True
return app_label == 'admission'
elif db == 'journals_db':
return False
return None
$ python manage.py migrate admission
$ python manage.py migrate
Answer the question
In order to leave comments, you need to log in
The migrate management command operates on one database at a time. By default, it operates on the default
If, as in the second example above, you've left the default database empty, you must provide a database name each time you run migrate
you can write an alias for all databases or write your own command wrapper
managed to get around the error with
now Django thinks that the migrations are done and there is no error, but I would like to try to find a solution so that you can just run migrate and not take a steam bath
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question