我试图将外部mysql数据库连接到Django,仅用于读取数据。我已按照Django Doc的说明进行操作,以连接多数据库。但是,当我尝试makemigration
/ migrate
新数据库时,它返回错误,如下所示。我在Google上做了一些研究,发现那里没有很多解决方案可以解决我的问题。这是我第一次看到这种情况,却不知道导致错误的原因是什么。我通过inspectdb
导入模型,TotalopennumTmp是数据库中的模型之一。感谢您帮助我解决此问题。
ERROR:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/app/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/app/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/home/app/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/app/venv/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/home/app/venv/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/home/app/venv/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/app/app/models.py", line 1007, in <module>
class TotalopennumTmp(models.Model):
File "/home/app/venv/lib/python3.6/site-packages/django/db/models/base.py", line 309, in __new__
new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
File "/home/app/venv/lib/python3.6/site-packages/django/apps/registry.py", line 221, in register_model
(model_name, app_label, app_models[model_name], model))
RuntimeError: Conflicting 'totalopennumtmp' models in application 'app': <class 'app.app.models.TotalOpennumTmp'> and <class 'app.app.models.TotalopennumTmp'>.
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'local',
#skip the detail
},
'afe':{
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myapp_db',
'USER': 'root',
'PASSWORD': 'pwd',
'HOST': 'mysql_host',
'PORT': 'mysql_port',
}
}
DATABASE_ROUTERS = ['myapp.routers.MyAppRouter',]
routers.py
class MyAppRouter:
def db_for_read(self, model, **hints):
if model._meta.app_label == 'myapp':
return 'myapp_db'
return None
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'myapp' or \
obj2._meta.app_label == 'myapp':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'myapp':
return db == 'myapp_db'
return None
def allow_syncdb(self, db, model):
#make sure myapp only appear on myapp_db db
if db == 'myapp_db':
return model._meta.app_label == 'myapp'
elif model._meta.app_label == 'myapp':
return False
return None
models.py
class TotalOpennumTmp(models.Model):
#some field
class Meta:
managed = False
db_table = 'total_opennum_tmp'
class TotalopennumTmp(models.Model):
#some field
class Meta:
managed = False
db_table = 'totalopennum_tmp'