【发布时间】:2017-01-14 17:36:08
【问题描述】:
我正在使用两个数据库,其中一个是 Mysql,另一个是 PostgreSql。
Mysql 定义为'default',PostgreSql 定义为'location_db'
我有 5 个应用程序,比如说“a、b、c、d、位置”应用程序,
所有应用都应该在“默认”数据库上运行所有东西,除了位置应用,
我只是执行 'python manage.py runserver' 然后一切正常,甚至站点也正常工作,但是当我在管理页面然后单击 'location' 管理模型时,它运行错误 *关系“location_locationmodel”不存在; LINE 1: SELECT COUNT(*) AS "__count" FROM "location_locationmodel" * 另一方面,如果我点击 'abcd' 管理模型,它就可以正常工作。
1)这是 router.py(我只是从 doc.django 站点获取路由器代码,可能代码不适合我,因为我只是猜测我的路由器配置不正确?也许?)
class LocationRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to location_db.
"""
if model._meta.app_label == 'location':
return 'location_db'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to location_db.
"""
if model._meta.app_label == 'location':
return 'location_db'
return None
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the events app is involved.
"""
if obj1._meta.app_label == 'location' or \
obj2._meta.app_label == 'location':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth app only appears in the 'location_db'
database.
"""
if app_label == 'location':
return db == 'location_db'
return None
2) 这是settings.py
DATABASE_ROUTERS = ['location.router.LocationRouter']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'my.cnf'),
},
},
'location_db': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geo',
'USER': 'john',
'PASSWORD': 'toor',
'HOST': 'localhost',
'PORT': '5432',
},
}
* 我的预测 *
1) 如果我创建另一个项目并且只使用 PostgreSql,那么一切都可以正常工作,所以所有数据库都可以正常工作
2) 这是我执行“python manage.py makemigrations or migrate”的时候:
Operations to perform:
Apply all migrations: admin, auth, a, contenttypes, location, b, c, d, registration, sessions, sites
Running migrations:
No migrations to apply.
* 我认为迁移的结果不应该与“位置”应用一起提供,或者可能来自 。这只是预测,我真的不知道,但是当我在管理页面上单击“位置”管理模型时,它会运行错误,但当我只使用一个数据库时它不会运行错误,我的意思是它是否仍在搜索表在mysql数据库里面?可能是路由器配置不正确? *
【问题讨论】:
标签: python django django-models django-admin