django fails to retrieve data using multiple databases
Im trying to access one database table which is in several databases.
I tried using MytableObject.get.using(databasename) first, but it threw error:
django.db.utils.DatabaseError: relation "mytable" does not exist
So i set out trying to figure out why. as long as i know im not doing anything different than usual :
DATABASES = {
'default': {
'ENGINE': 'django开发者_StackOverflow中文版.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': config.get('databaseDefault', 'DATABASE_NAME'), # Or path to database file if using sqlite3.
'USER': config.get('databaseDefault', 'DATABASE_USER'), # Not used with sqlite3.
'PASSWORD': config.get('databaseDefault', 'DATABASE_PASSWORD'), # Not used with sqlite3.
'HOST': config.get('databaseDefault', 'DATABASE_HOST'), # Set to empty string for localhost. Not used with sqlite3.
'PORT': config.get('databaseDefault', 'DATABASE_PORT'), # Set to empty string for default. Not used with sqlite3.
},
'databaseEe': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': config.get('databaseEe', 'DATABASE_NAME'), # Or path to database file if using sqlite3.
'USER': config.get('databaseEe', 'DATABASE_USER'), # Not used with sqlite3.
'PASSWORD': config.get('databaseEe', 'DATABASE_PASSWORD'), # Not used with sqlite3.
'HOST': config.get('databaseEe', 'DATABASE_HOST'), # Set to empty string for localhost. Not used with sqlite3.
'PORT': config.get('databaseEe', 'DATABASE_PORT'), # Set to empty string for default. Not used with sqlite3.
},
'databaseLv': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': config.get('databaseLv', 'DATABASE_NAME'), # Or path to database file if using sqlite3.
'USER': config.get('databaseLv', 'DATABASE_USER'), # Not used with sqlite3.
'PASSWORD': config.get('databaseLv', 'DATABASE_PASSWORD'), # Not used with sqlite3.
'HOST': config.get('databaseLv', 'DATABASE_HOST'), # Set to empty string for localhost. Not used with sqlite3.
'PORT': config.get('databaseLt', 'DATABASE_PORT'), # Set to empty string for default. Not used with sqlite3.
},
'databaseLt': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': config.get('databaseLt', 'DATABASE_NAME'), # Or path to database file if using sqlite3.
'USER': config.get('databaseLt', 'DATABASE_USER'), # Not used with sqlite3.
'PASSWORD': config.get('databaseLt', 'DATABASE_PASSWORD'), # Not used with sqlite3.
'HOST': config.get('databaseLt', 'DATABASE_HOST'), # Set to empty string for localhost. Not used with sqlite3.
'PORT': config.get('databaseLt', 'DATABASE_PORT'), # Set to empty string for default. Not used with sqlite3.
}
}
And i created some testcode:
from django.db import connections
cursor = connections[database].cursor()
cursor.execute("select tablename from pg_tables WHERE tablename !~* 'pg_*'")
print cursor.fetchall()
which printed out all my tablenames, "mytable" among them.
Then i tried this code :
from django.db import connections
cursor = connections[database].cursor()
cursor.execute("select * from mytable")
print cursor.fetchall()
which produced error again:
django.db.utils.DatabaseError: relation "mytable" does not exist
LINE 1: select * from mytable
So what am i doing wrong? i can read and write into my default database just fine. I tracked things down to this line:
cursor = connections[database].cursor()
and found that, if i write actual connection name instead of database, like that:
cursor = connections['databaseLv'].cursor()
then it works just fine. So how can i do this dynamically?
Alan
Django needs DB Routers to be defined to decide what database to use, maybe your definitions are not the right ones.
精彩评论