2012-01-17 6 views
1

デフォルトのデータベースにクエリセットを使用できます。 しかし、別のデータベースにクエリセットを使用すると、例外がスローされます。djangoの複数のデータベースに対してクエリセットを使用できません

私のアプリケーションでは、2つのデータベースを使用しています。 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. 
      } 
} 

私はそれがどんな例外をスローされていない最初のデータベースのクエリセットを使用します。 2番目のデータベースを使用している間は、スローテーブルは使用できません。返信トム、 ため

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

+1

ルーター(https://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-routers)を作成しましたか?おそらくテーブルはデータベースのうちの1つにしかありません(照会しようとするとDjangoがスローするのではなく)。 – Tom

+0

database1には異なるテーブルがあり、database2には異なるテーブルがあります。 – sreekanth

答えて

1

おかげで私は手動で第二のデータベースを試してみました、それが私のために働きました。

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

これは、すべてのテーブルが両方のデータベースに存在しない場合に使用できます。

デフォルトのデータベースを使用する場合は、(使用)を使用する必要はありません。 デフォルトのデータベースから直接照会します。

0

ジャンゴで複数のデータベースを使用するのが最善のオプションも、複数のDBを管理するルーティングを使用され、ここでドキュメント:ここ

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はテーブル名に基づいて正しいデータベースを使用します。

希望:

関連する問題