Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango fails to retrieve data using multiple databases
    text
    copied!<p>Im trying to access one database table which is in several databases.</p> <p>I tried using MytableObject.get.using(databasename) first, but it threw error:</p> <pre><code>django.db.utils.DatabaseError: relation "mytable" does not exist </code></pre> <p>So i set out trying to figure out why. as long as i know im not doing anything different than usual :</p> <pre><code>DATABASES = { 'default': { 'ENGINE': 'django.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. } } </code></pre> <p>And i created some testcode:</p> <pre><code>from django.db import connections cursor = connections[database].cursor() cursor.execute("select tablename from pg_tables WHERE tablename !~* 'pg_*'") print cursor.fetchall() </code></pre> <p>which printed out all my tablenames, "mytable" among them.</p> <p>Then i tried this code :</p> <pre><code>from django.db import connections cursor = connections[database].cursor() cursor.execute("select * from mytable") print cursor.fetchall() </code></pre> <p>which produced error again:</p> <pre><code>django.db.utils.DatabaseError: relation "mytable" does not exist LINE 1: select * from mytable </code></pre> <p>So what am i doing wrong? i can read and write into my default database just fine. I tracked things down to this line:</p> <pre><code>cursor = connections[database].cursor() </code></pre> <p>and found that, if i write actual connection name instead of database, like that:</p> <pre><code>cursor = connections['databaseLv'].cursor() </code></pre> <p>then it works just fine. So how can i do this dynamically?</p> <p>Alan</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload