【发布时间】:2012-02-12 15:27:42
【问题描述】:
我可以为默认数据库使用查询集。 但是当我对另一个数据库使用查询集时,抛出异常。
在我的应用程序中,我使用了两个数据库。 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
【问题讨论】:
-
您确定该表存在于数据库中吗?
-
您是否创建了路由器(docs.djangoproject.com/en/dev/topics/db/multi-db/#using-routers)?很可能该表仅在其中一个数据库中(而不是 Django 在尝试查询时抛出)。
-
database1 有不同的表,database2 有不同的表。
-
database1 有不同的表,database2 有不同的表。当我运行以下命令时,shell = db2models.tablename.objects.get(name='abc') 抛出异常 DatabaseError: no such table: xyz
标签: python django django-queryset django-database