1

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

Running: python manage.py shell_plus --settings=settings.local_oleg

In the shell:

>>> 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

While if I define the mySQL database as a single, default database, everything works fine.

Can post the user model if necessary, although I don't thin that it is relevant.

6
  • I'm not sure routers can be referenced by relative module name, try using the fully-qualified module name in DATABASE_ROUTERS Commented Feb 6, 2014 at 12:35
  • Moved the router into the legacy app, and changed the path to legacy.integrationRouter, and got a different error, updated the post. Commented Feb 6, 2014 at 12:49
  • If your module file is legacy/integrationRouter.py, and your class name is integrationRouter, then your setting should be DATABASE_ROUTERS = ['legacy.integrationRouter.integrationRouter']; or alternatively, you can add a from .integrationRouter import integrationRouter line to your legacy/__init__.py file. Commented Feb 6, 2014 at 12:52
  • raise ValueError("fallback required, but not specified")ValueError: fallback required, but not specified :( Commented Feb 6, 2014 at 12:58
  • Full traceback: code.stypi.com/bfz5quih Commented Feb 6, 2014 at 13:01

1 Answer 1

1
DATABASE_ROUTERS = ['.integrationRouter']

The DATABASE_ROUTERS setting cannot reference relative module names. Replace your setting with a fully-qualified module and class name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.