Opened 6 years ago

Closed 6 years ago

#20704 closed Bug (worksforme)

DATABASE_ROUTERS : ImproperlyConfigured "cannot import name connection"

Reported by: artscoop Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords: router
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This bug occurs also on other versions of Django.
I have a Django project which usually works well.
Today I tried to introduce the use of another database. (one is PostgreSQL, the new one is SQLite)

I've setup my as follows :

DATABASE_ROUTERS = ['engine.db.routers.custom.Router',]

The path to the router is fine, because changing it to a non-existing one raises a correct error. The router inherits from object, and defines the 4 functions shown in the Django documentation. The
allow_relation and allow_syncdb members return True (tried with None), db_for_write and db_for_read return a database name or None.
I have properly defined my DATABASE setting.

However, trying to syncdb throws

  File "/home/artscoop/virtualenv/project/lib/python2.7/site-packages/django/db/", line 16, in <module>
    router = ConnectionRouter(settings.DATABASE_ROUTERS)
  File "/home/artscoop/virtualenv/project/lib/python2.7/site-packages/django/db/", line 117, in __init__
    raise ImproperlyConfigured('Error importing database router %s: "%s"' % (klass_name, e))
django.core.exceptions.ImproperlyConfigured: Error importing database router Router: "cannot import name connection"

No answer in any site could resolve this problem.
The dubious advice to add from django.db import connections leads to another exception

  File "/home/steve/virtualenv/project/lib/python2.7/site-packages/django/db/backends/dummy/", line 15, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

Change History (7)

comment:1 Changed 6 years ago by artscoop

I have found that moving the router to an app which has no (or is not in INSTALLED_APPS) fixes everything. But the problem with importing from another location is still a bug which is at least not documented.

Last edited 6 years ago by artscoop (previous) (diff)

comment:2 Changed 6 years ago by Aymeric Augustin

Looks like a circular import.

comment:3 Changed 6 years ago by Simon Charette

@artscoop what are you importing in your routers module?

comment:4 Changed 6 years ago by Claude Paroz

This might have been fixed by commit [6a6bb168be90594a18]. To check this, it would be nice if you could test with the current 1.6beta.

comment:5 Changed 6 years ago by artscoop

The module does not import anything.
Only has

# coding: utf-8
from __future__ import absolute_import

comment:6 Changed 6 years ago by artscoop

The behaviour of routers seems correct in @1.6b1 and @stable/1.6.x though.

Last edited 6 years ago by artscoop (previous) (diff)

comment:7 Changed 6 years ago by Claude Paroz

Resolution: worksforme
Status: newclosed

Thanks for testing on latest code, and I'm happy this is fixed there.

I think you should be able to temporarily workaround the problem by moving your custom Router class in a different path. The current circular import issue may be caused by any along the path (i.e. engine/, engine/db/, etc.).

I really doubt that the fix will be backported in 1.5, therefore closing the ticket.

Note: See TracTickets for help on using tickets.
Back to Top