diff --git a/django/db/utils.py b/django/db/utils.py index c3ee34031a..2917dbee96 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -32,6 +32,8 @@ def load_backend(backend_name): else: raise # If there's some other error, this must be an error in Django itself. +class ConnectionDoesNotExist(Exception): + pass class ConnectionHandler(object): def __init__(self, databases): @@ -43,7 +45,10 @@ class ConnectionHandler(object): Puts the defaults into the settings dictionary for a given connection where no settings is provided. """ - conn = self.databases[alias] + try: + conn = self.databases[alias] + except KeyError: + raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias) conn.setdefault('DATABASE_ENGINE', 'dummy') conn.setdefault('DATABASE_OPTIONS', {}) conn.setdefault('TEST_DATABASE_CHARSET', None) diff --git a/docs/topics/db/multi-db.txt b/docs/topics/db/multi-db.txt index 5a76848b5b..58fd6af8a9 100644 --- a/docs/topics/db/multi-db.txt +++ b/docs/topics/db/multi-db.txt @@ -23,7 +23,9 @@ settings for that specific connection. The settings in the inner dictionaries are described fully in the :settings:`DATABASES` documentation. The important thing to note is that your primary database should have the alias ``'default'``, and any additional databases you have can have whatever alias -you choose. +you choose. If at any time you attempt to access a database that isn't defined +in your :settings:`DATABASES` setting then Django will raise a +``django.db.utils.ConnectionDoesNotExist`` exception. Selecting a Database for a ``Model`` ==================================== @@ -62,4 +64,4 @@ keyword argument to ``save()``. For example if you were migrating a user from the ``'legacy_users'`` database to the ``'new_users'`` database you might do:: >>> user_obj.save(using='new_users') - >>> usre_obj.delete(using='legacy_users') + >>> user_obj.delete(using='legacy_users')