'Django Database Routers
I have set up routers in my Django project, but the tables do not seem to be populating in any of the databases. Each time I run python manage.py makemigrations HealthcareProject4 I can see that the migration file is being created successfully. However when I run python manage.py migrate --database=default nothing appears to populate in any of the databases I have set up.
db_routers.py
from HealthcareProject4.settings import DATABASE_ROUTERS
from HealthcareProject4.Conditions.models import *
class AuthRouter:
route_app_labels = ['auth', 'contenttypes', 'admin',]
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return model._meta.app_label
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return model._meta.app_label
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == app_label
return None
class EducationRouter:
route_app_labels = ['EducationProject']
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return model._meta.app_label
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return model._meta.app_label
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == app_label
return None
class HealthcareRouter:
route_app_labels = ['HealthcareProject4']
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return model._meta.app_label
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return model._meta.app_label
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == app_label
return None
class UsersRouter:
route_app_labels = ['Users2']
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return model._meta.app_label
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return model._meta.app_label
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == model._meta.app_label
return None
settings.py
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_Healthcare4',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'crm': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_CRM',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'education': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_Education',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'finance': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_Finance',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'legals': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_Legals',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'machinelearning': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_MachineLearning',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'media': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_Media',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'music': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_Music',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'socialservices': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_SocialServices',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'users': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_Users',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
'auth': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'NKFSLTD_Auth',
'USER': 'Nf00038',
'PASSWORD': '',
'HOST': '192.168.1.116',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
},
}
DATABASE_ROUTERS = ['Routers.db_routers.AuthRouter', 'Routers.db_routers.EducationRouter', 'Routers.db_routers.HealthcareRouter',]
models.py
from django.db import models
# Create your models here.
class TestModel(models.Model):
Test = models.CharField(max_length=20)
class Meta:
app_label = 'EducationProject'
Solution 1:[1]
The methods of the router db_for_read and db_for_write must return the name of the database that will be used and not the app label. You must return a key used in the DATABASES dict (one of the following: default, crm, education, finance, legals, machinelearning, media, music, socialservices, users, auth). The same applies for the method allow_migrate, you must compare db with the database name and not the app label. So for example:
class EducationRouter:
route_app_labels = ['EducationProject']
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'education' # Here
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'education' # Here
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == 'education' # Here
return None
Then run the following commands:
python manage.py makemigrations
python manage.py migrate
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
