首页 文章

Django多个数据库 - 关系不存在;第1行:SELECT COUNT(*)AS“__count”FROM

提问于
浏览
2

我使用的是两个数据库,其中一个是Mysql,另一个是PostgreSql .

  • Mysql是为'default'定义的,PostgreSql是为'location_db'定义的

  • 我有5个app,让我们说'a,b,c,d,location'应用,

  • 除了位置应用程序外,所有应用程序都应运行“默认”数据库上的所有内容

  • 我只是做' python manage.py runserver '然后一切正常,甚至网站工作正常,但当我在管理页面然后单击' location '管理模型它运行错误 *** relation "location_locationmodel" does not exist; 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',
    },
}

*** My Predictions ***

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.

*** outcomes of migrate I think should not comes with 'location' app or maybe come** . 这只是预测我在管理页面上运行错误但是当我只使用一个数据库时它没有运行错误 **** **I mean does it searches the table still inside mysql database? could be the router is not configured properly ? ***

1 回答

  • 1

    Okey Guys解决方案并不是太远,如果你没有失眠,在这种情况下你只是指明一切

    • python manage.py迁移位置--database = location_db

    在这里,你的查询匹配postgresql数据库:)祝你好运

相关问题