我正在使用两个数据库,一个 Mysql 另一个是 PostgreSql。
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 或 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 manage.py migrate --run-syncdb
关于python - Django 多个数据库 - 关系不存在;第 1 行 : SELECT COUNT(*) AS "__count" FROM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652951/