Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26773 closed Uncategorized (invalid)

DB Router not applied in introspection

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

Description

I am using multiple databases in my Django project. I do not have a default database, but I implemented a database router as documented here https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#database-routers

I also have a management command which needs to check whether a Table exists or not. I did that basically with

from django.db import connection
table_name in connection.introspection.table_names()

Now I get following error:

django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

As it seems, the DatabaseRouter does not work correctly. Neither db_for_read() nor db_for_write() is called at that point.

Change History (3)

comment:1 by Tim Graham, 8 years ago

Can you provide a sample project to reproduce the crash? Not sure we have enough information to reproduce this otherwise.

comment:2 by Simon Charette, 8 years ago

Resolution: invalid
Status: newclosed

Since you don't have a default database you can't use the django.db.connection object as it simply an alias to django.db.connections['default']. Routers only work at the ORM and migration level by mapping models and action to a connection alias. If you use the low level connection objects you're on your own here.

In your case you'll have to iterate over django.db.connections to retrieve table names:

from django.db import connections

table_names = {}
for alias in connections:
    table_names[alias] = connections[alias].introspection.table_names()

Please re-open if this doesn't match your use case.

comment:3 by Tim Graham, 8 years ago

Component: UncategorizedDatabase layer (models, ORM)
Summary: DB Router not applied thoroughlyDB Router not applied in introspection
Note: See TracTickets for help on using tickets.
Back to Top