The goal is to be able to have an app that all models in that app go to a seperate database, other models like the django-admin, go to default.
I expect that when I target no database it migrates all existing migrations, not in my new app, to the default database connection.
Then when I run ./manage.py migrate --database=otherdatabase it only migrates those models in the new app, or none at all.
This could be a flawed expectation, but it is my understanding of what should happen.
No matter what database I use when doing a ./manage.py migrate it is migrating all tables to the database.
- Install create virtualen environment
- Setup a postgres database. I am doing this throud docker
version: "3"
services:
db:
image: postgres:10.1-alpine
environment:
POSTGRES_DB: "database1"
POSTGRES_USER: "postgres"
ports:
- 5432:5432
- Install
psycopg2-binaryandDjango django-admin.py startproject routing- Edit
routing/settings.pyadding the following to the database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'database1',
'USER': 'postgres',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
},
'database2': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'database2',
'USER': 'postgres',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
- Create database
database2in postgres - Create a new app
./manage.py startapp integrations - Add
integrationsapp toINSTALLED_APPSinsettings.py - Create file
router.pyinintegrationsapp - Copy the following code into it. It is just a modification of what is in the Django docs.
APP_LABEL = "integrations"
DB_CONNECTION_NAME = "database2"
class IntegrationRouter:
def db_for_read(self, model, **hints):
if model._meta.app_label == APP_LABEL:
return DB_CONNECTION_NAME
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == APP_LABEL:
return DB_CONNECTION_NAME
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == APP_LABEL or obj2._meta.app_label == APP_LABEL:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == APP_LABEL:
return db == DB_CONNECTION_NAME
return None
- Add the following to
settings.pyfor the database router to be used
DATABASE_ROUTER = ['integrations.router.IntegrationRouter']
- Run migration command
./manage.py migrate. It should run all migrations - Run migration command
./manage.py migrate --database=database2. No migrations should be run, but on my system they are.
Me too. How could I fix it?