2012-01-17 20 views
1

我能夠使用默認數據庫的查詢集。 但是,當我使用查詢設置爲另一個數據庫,拋出異常。不能使用查詢設置爲Django中的多個數據庫

在我的應用程序中,我使用了兩個數據庫。 SQLite和mysql的

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 
     'NAME': 'abc.db',      # Or path to database file if using sqlite3. 
     'USER': '',      # Not used with sqlite3. 
     'PASSWORD': '',     # Not used with sqlite3. 
     'HOST': '',      # Set to empty string for localhost. Not used with sqlite3. 
     'PORT': '',      # Set to empty string for default. Not used with sqlite3. 
    }, 
     'second' : { 
     'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 
     'NAME': 'abc',      # Or path to database file if using sqlite3. 
     'USER': 'abcdb',      # Not used with sqlite3. 
     'PASSWORD': 'xxxxx',     # Not used with sqlite3. 
     'HOST': '',      # Set to empty string for localhost. Not used with sqlite3. 
     'PORT': '',      # Set to empty string for default. Not used with sqlite3. 
      } 
} 

當我使用的查詢設置第一個數據庫它不拋出任何異常。 雖然使用第二個數據庫它是投擲表不可用。

TemplateSyntaxError at /abc/xyz/ 

Caught DatabaseError while rendering: no such table: second.tablename 

Request Method:  GET 
Request URL: http://127.0.0.1:8000/xxx/yyyy/?q=abcd 
Django Version:  1.3.1 
Exception Type:  TemplateSyntaxError 
Exception Value:  

Caught DatabaseError while rendering: no such table: second.tablename 
+1

你確定該表在數據庫中存在? – dm03514 2012-01-17 20:29:13

+1

您是否創建了路由器(https://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-routers)?這表可能只在其中一個數據庫中(而不是Django在嘗試查詢時拋出)。 – Tom 2012-01-17 20:33:35

+0

database1有不同的表,database2有不同的表。 – sreekanth 2012-01-18 08:53:09

回答

1

感謝您的答覆湯姆, 我已經試過手動第二個數據庫,它爲我工作。

$model_seconddb.modelname.objects.using('seconddatabasename').filter(name='xxx') 

當兩個數據庫中的所有表都不存在時,可以使用此選項。

當您要使用默認數據庫時,不需要使用(使用)。它會直接從默認數據庫查詢。

0

最好的選擇使用多個數據庫Django中還使用路由管理多個數據庫,這裏的文檔:

https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#topics-db-multi-db-routing

這裏從文件一個完整的例子:

class AuthRouter(object): 
""" 
A router to control all database operations on models in the 
auth application. 
""" 
def db_for_read(self, model, **hints): 
    """ 
    Attempts to read auth models go to auth_db. 
    """ 
    if model._meta.app_label == 'auth': 
     return 'auth_db' 
    return None 

def db_for_write(self, model, **hints): 
    """ 
    Attempts to write auth models go to auth_db. 
    """ 
    if model._meta.app_label == 'auth': 
     return 'auth_db' 
    return None 

def allow_relation(self, obj1, obj2, **hints): 
    """ 
    Allow relations if a model in the auth app is involved. 
    """ 
    if obj1._meta.app_label == 'auth' or \ 
     obj2._meta.app_label == 'auth': 
     return True 
    return None 

def allow_migrate(self, db, app_label, model_name=None, **hints): 
    """ 
    Make sure the auth app only appears in the 'auth_db' 
    database. 
    """ 
    if app_label == 'auth': 
     return db == 'auth_db' 
    return None 

如果不在查詢中指定數據庫,Django將在表名的基礎上使用正確的數據庫。

希望這有助於:)