neghez
neghez

Reputation: 725

Specify database to use in Django

I need to use multiple databases for my django project. The application works fine when there is only one database:

In setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',                     
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',                     
        'PORT': 3306,    
    },

But if I added more databases from the same engine:

DATABASES = {
    'default':{},
    'mydb1': {
            'ENGINE': 'django.db.backends.mysql', 
            'NAME': 'mydb1',    
            'USER': 'root',
            'PASSWORD': '',
            'HOST': '127.0.0.1',                    
            'PORT': 3306,                  
        },
   'mydb2': {
            'ENGINE': 'django.db.backends.mysql', 
            'NAME': 'mydb2',           
            'USER': 'root',
            'PASSWORD': '',
            'HOST': '127.0.0.1',             
            'PORT': 3306,                   
                }
    }

it gives me following error:

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

Also, i tried:

DATABASES = {
        'default': {
                'ENGINE': 'django.db.backends.mysql', 
                'NAME': 'mydb1',    
                'USER': 'root',
                'PASSWORD': '',
                'HOST': '127.0.0.1',                    
                'PORT': 3306,                  
            },
       'mydb2': {
                'ENGINE': 'django.db.backends.mysql', 
                'NAME': 'mydb2',           
                'USER': 'root',
                'PASSWORD': '',
                'HOST': '127.0.0.1',             
                'PORT': 3306,                   
                    }
        }

It only sees mydb1, not mydb2, when i tried query mydb2, it gives me:

DoesNotExist: Site matching query does not exist.

Do I need to define database route? it seems that I only need to do that for customized read/write.

Thanks

UPDATE:

In django docs, it says "The default routing scheme ensures that if a database isn't specified, all queries fall back to the default database".

So I guess my actual question is how do I specify a database to use for my queries?

Upvotes: 3

Views: 1435

Answers (1)

alko
alko

Reputation: 48317

It is explicetely stated in docs

The DATABASES setting must configure a default database; any number of additional databases may also be specified.

If the concept of a default database doesn’t make sense in the context of your project, you need to be careful to always specify the database that you want to use.

As in your second example default database is not configured

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

when you access your data with no database specified, a django.db.backends.dummy backend is used, which complains on your configuration with ImproperlyConfigured error.

An example of configuring multiple database usage with Database Routers can be found in docs

update

Site matching query error is for completely different reasons, and is another question. Answer here, as it is duplicate of many others: as your mysql1 and mysql2 dbs have different content, second one seems to not to be properly configured. Refer site matching query does not exist.

Upvotes: 1

Related Questions