【问题标题】:django fails to retrieve data using multiple databasesdjango 无法使用多个数据库检索数据
【发布时间】:2011-09-26 13:59:01
【问题描述】:

我正在尝试访问多个数据库中的一个数据库表。

我首先尝试使用 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()

然后它工作得很好。那么我该如何动态地做到这一点呢?

艾伦

【问题讨论】:

    标签: database django multiple-databases


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      相关资源
      最近更新 更多