33

I have been using manual db selection to cope with a project which has two seperate dbs. I have defined my databases in the settings.

After some further reading it seems that database routing is actually the way to go with this. However, after reading the docs and some relevant posts here I am more confused than ever.

In my settings I have:

DATABASES = {
    'default': {
       .... 
    },
    'my_db2': {
       ....
    }
}

DATABASE_ROUTERS = ['myapp2.models.MyDB2Router',]

I know I have to define my router class (I think in myapp2.models.py file) like so:

class MyDB2Router(object):
"""A router to control all database operations on models in
the myapp2 application"""

def db_for_read(self, model, **hints):
    if model._meta.app_label == 'myapp2':
        return 'my_db2'
    return None

def db_for_write(self, model, **hints):
    if model._meta.app_label == 'myapp2':
        return 'my_db2'
    return None

def allow_relation(self, obj1, obj2, **hints):
    if obj1._meta.app_label == 'myapp2' or obj2._meta.app_label == 'myapp2':
        return True
    return None

def allow_syncdb(self, db, model):

    if db == 'my_db2':
        return model._meta.app_label == 'myapp2'
    elif model._meta.app_label == 'myapp2':
        return False
    return None

Then what? Does each model require a meta.app_label or is that automatic? Aside from that, I still get an error:

django.core.exceptions.ImproperlyConfigured: Error importing database router JournalRouter: "cannot import name connection

Can anyone help me understand what is happening and what is going wrong? Any help much appreciated.

3
  • 8
    OK, so I just solved my own problem. The router class goes into a separate file called routers.py under /myapp2. No meta.app_label is required as I guess it is automatically assigned. Hope this helps someone. Nov 8, 2011 at 17:12
  • 3
    Create a new answer to your question and accept it then. Nov 8, 2011 at 18:32
  • You only need to use the app_label option if the model is defined outside of the models.py, otherwise it is assigned automatically.
    – Alasdair
    Nov 9, 2011 at 0:12

4 Answers 4

31

OK, so I just solved my own problem. The router class goes into a separate file called routers.py under /myapp2. No meta.app_label is required as I guess it is automatically assigned. Hope this helps someone. I have also documented the process here.

1
  • 11
    Accept your answer so that will be easier for others to unnderstand that this is a correct answer.
    – arulmr
    Sep 14, 2012 at 9:54
6

Did not help me, so I did some debugging. Maybe the results can save someone some pain. :) The problem in django 1.4 is a circular reference that occurs when django tries to import the custom router class.
This happens in django.db.utils.ConnectionRouter. In my case the app's __init__.py imported a module (tastypie.api to be precise) that in turn (and through a long chain) imported django.db.models. That is not bad in itself, but models tries to import connection from django.db and that happens to have a dependency on ConnectionRouter. Which is exactly where our journey started. Hence the error.

This is described as a bug in django < 1.6 here: https://code.djangoproject.com/ticket/20704 and there is a nice small changeset thats supposed to fixed it in django 1.6:https://github.com/django/django/commit/6a6bb168be90594a18ab6d62c994889b7e745055

My solution however was to simply move routers.py from the app directory to the project directory. No nasty dependencies there.

2
  • This is what fixed it for me (along with the tip from @Janosch)
    – hobs
    Sep 20, 2013 at 0:45
  • I moved my routing class into my main project and this fixed the "cannot import name connection" error for me too. Thank you! Oct 27, 2013 at 10:50
4

One more mistake to ommit, is to import the models in the router, this will lead to the same error, even if the router is defined in a different file.

1
  • And even something as simple as from app.models import some_fun will cause this error (in django 1.5).
    – hobs
    Sep 20, 2013 at 0:44
3

If you have one app that uses multiple databases you can route on a per application and per table basis. For example if your app is "console" and you only want the "PoolServers" model to come from a different back end you would put this in your routers.py

class PoolServerRouter(object): 
def db_for_read(self, model, **hints):
    "Point only reads to poolserver  model to 'hamburger'"
    if model._meta.app_label == 'console' and model._meta.db_table == 'PoolServers':
        return 'hamburger'
    return 'default'

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.