0

我试图访问位于多个数据库中的一个数据库表。

我首先尝试使用 MytableObject.get.using(databasename) ,但它抛出了错误:

django.db.utils.DatabaseError: relation "mytable" does not exist

所以我开始试图找出原因。只要我知道我没有做任何与平时不同的事情:

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.
    }                    
}

我创建了一些测试代码:

from django.db import connections
cursor = connections[database].cursor()
cursor.execute("select tablename from pg_tables WHERE tablename !~* 'pg_*'")
print cursor.fetchall()

它打印出我所有的表名,其中包括“mytable”。

然后我尝试了这段代码:

from django.db import connections
cursor = connections[database].cursor()
cursor.execute("select * from mytable")
print cursor.fetchall()

再次产生错误:

django.db.utils.DatabaseError: relation "mytable" does not exist
LINE 1: select * from mytable

那我做错了什么?我可以很好地读取和写入我的默认数据库。我追踪到这一行:

cursor = connections[database].cursor()

并发现,如果我写实际的连接名称而不是数据库,就像这样:

cursor = connections['databaseLv'].cursor()

然后它工作得很好。那么我怎样才能动态地做到这一点?

艾伦

4

1 回答 1

0

Django 需要定义 DB 路由器来决定使用什么数据库,也许你的定义不正确。

于 2012-04-11T17:57:31.983 回答