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?

Community
  • 1
  • 1
neghez
  • 715
  • 1
  • 8
  • 15

1 Answers1

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.

Community
  • 1
  • 1
alko
  • 46,136
  • 12
  • 94
  • 102
  • The second example django docs gives for multiple databases left it empty: https://docs.djangoproject.com/en/1.5/topics/db/multi-db/ – neghez Nov 13 '13 at 21:40
  • 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? – neghez Nov 13 '13 at 22:02
  • @zyenge see last link to docs in my answer, this **can't be done** in model, it is done via `DATABASE_ROUTERS` setting – alko Nov 13 '13 at 22:04
  • So customized db_for_read/write or not, a `DATABASE_ROUTERS` **must** be used when there are multiple databases? That was my original question... – neghez Nov 13 '13 at 22:10
  • @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. – alko Nov 13 '13 at 22:20
  • Sorry, I wasn't clear about the question. I thought DATABASE_ROUTERS is used when I have multiple databases in my backend **and** I want to specify a database to write to, and another one to read from. But if all I need is to access all my databases, I don't need to customize which one to read, which one to write, do I still need to define DATABASE_ROUTERS? Is there a way to access multiple databases as if all tables are in one db? – neghez Nov 13 '13 at 22:27