3

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?

1
  • Did you find out how to select the database in the end? Commented Apr 12, 2022 at 21:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

The second example django docs gives for multiple databases left it empty: docs.djangoproject.com/en/1.5/topics/db/multi-db
I can specify database to update when syncdb --databases=mydb2, but how do i specify a database while accessing data? Is it done in model.py?
@zyenge see last link to docs in my answer, this can't be done in model, it is done via DATABASE_ROUTERS setting
So customized db_for_read/write or not, a DATABASE_ROUTERS must be used when there are multiple databases? That was my original question...
@zyenge Sorry, I don't get meaning of So customized db_for_read/write or not, a database_route must be used when there are multiple databases? And see updates about site not defined.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.