Reputation: 133
I have 2 mysql databases and I want to create a new model to the second one (analysis_db), but after running makemigrations, it says "No changes detected". Here are my codes
In settings.py ( I added myapp to INSTALLED_APPS )
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': DB_DEFAULT_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': DB_HOST,
'PORT': '',
'OPTIONS': {
'sql_mode': 'traditional',
}
},
'analysis_db': {
'ENGINE': 'django.db.backends.mysql',
'NAME': DB_ANALYSIS_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': DB_HOST,
'PORT': '',
'OPTIONS': {
'sql_mode': 'traditional',
}
}
}
DATABASE_ROUTERS = ['my_project.routers.TestRouter']
In routers.py
class TestRouter:
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
if model._meta.app_label == 'analysis_data':
return 'analysis_db'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label == 'analysis_data':
return 'analysis_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'analysis_data' or \
obj2._meta.app_label == 'analysis_data':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'analysis_data':
return db == 'analysis_db'
return None
In models.py
class TestModel(models.Model):
id = models.AutoField(primary_key=True)
val1 = models.IntegerField()
val2 = models.IntegerField()
class Meta:
app_label = 'analysis_data'
db_table = 'test_table_on_db'
However, if I remove app_label = 'analysis_data'
from models.py
, and run makemigrations
again, it works, but the table was created on the default db. Does anybody got the same problem?
Thanks :)
Upvotes: 3
Views: 214
Reputation: 8026
I think the "app_label" on your models needs to be set to the name of an app that is installed in your INSTALLED_APPS setting. You mention you added "myapp" to INSTALLED_APPS, but what about "analysis_data"?
If you're using the app name "analysis_data" and there is no app with that name then it won't make migrations for that app because it doesn't exist.
So to fix this you should move your model's for "analysis_data" to another Django App (you can have multiple apps in the same project) and explicitly add it to your INSTALLED_APPS. Then when you run python manage.py makemigrations analysis_data
it should work.
Upvotes: 1