【问题标题】:Django DB router, not working with 2 different databases (mySQL, PostgreSQL)Django DB 路由器,不适用于 2 个不同的数据库(mySQL、PostgreSQL)
【发布时间】:2014-03-03 09:47:24
【问题描述】:

local_oleg.py:

from local import *  # noqa

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'schema1',
        'USER': 'postgres',
        'PASSWORD': 'zazaking',
        'HOST': 'localhost',
        # 'PORT': '',
        # 'OPTIONS': {'autocommit': True},
    },
    'legacy': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'legact_db',
        'USER': 'root',
        'HOST': '127.0.0.1',
        'PASSWORD': '',
    }

}

DATABASE_ROUTERS = ['.integrationRouter']

integrationRouter.py:

class integrationRouter(object): 
    import ipdb; ipdb.set__trace();
    def db_for_read(self, model, **hints):
        "Read from legace db if the model = 'integration'"
        if model._meta.app_label == 'integration':
            return 'legacy'
        return 'default'

    def db_for_write(self, model, **hints):
        "Write db is always default db"
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        "Allow any relation if a both models in integration app"
        if obj1._meta.app_label == 'integration' and obj2._meta.app_label == 'integration':
            return True
        # Allow if neither is integration app
        elif 'integration' not in [obj1._meta.app_label, obj2._meta.app_label]: 
            return True
        return False#Don't allow relatioin between legacy and default dchemas

    def allow_syncdb(self, db, model):
        if db == 'legacy' or model._meta.app_label == "integration":
            return False # we're not using syncdb on our legacy database
        else: # but all other models/databases are fine
            return True

正在运行:python manage.py shell_plus --settings=settings.local_oleg

在外壳中:

>>> a = Users.objects.all()
>>> a
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/db/models/query.py", line 71, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/db/models/query.py", line 166, in iterator
    if connections[self.db].features.supports_select_related:
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/db/models/query.py", line 817, in db
    return self._db or router.db_for_read(self.model)
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/db/utils.py", line 239, in _route_db
    for router in self.routers:
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/utils/functional.py", line 49, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/db/utils.py", line 230, in routers
    router = import_by_path(r)()
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 21, in import_by_path
    module = import_module(module_path)
  File "/home/oleg/.virtualenvs/tomigo_core/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
ValueError: Empty module name

虽然我将 mySQL 数据库定义为单个默认数据库,但一切正常。

如有必要,可以发布用户模型,尽管我不认为它是相关的。

【问题讨论】:

  • 我不确定路由器是否可以被相关模块名引用,请尝试使用DATABASE_ROUTERS中的完全限定模块名
  • 将路由器移到旧版应用程序中,并将路径更改为legacy.integrationRouter,并得到不同的错误,更新帖子。
  • 如果你的模块文件是legacy/integrationRouter.py,你的类名是integrationRouter,那么你的设置应该是DATABASE_ROUTERS = ['legacy.integrationRouter.integrationRouter']或者,您可以将from .integrationRouter import integrationRouter 行添加到您的legacy/__init__.py 文件中。
  • raise ValueError("fallback required, but not specified")ValueError: fallback required, but not specified :(
  • 完整回溯:code.stypi.com/bfz5quih

标签: python django postgresql integration django-database


【解决方案1】:
DATABASE_ROUTERS = ['.integrationRouter']

DATABASE_ROUTERS 设置不能引用相对模块名称。用完全限定的模块和类名替换您的设置。

【讨论】:

    猜你喜欢
    • 2012-10-03
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多