我有几个具有不同数据库连接的应用程序。他们都工作正常。
我的新应用程序需要一个Oracle连接,该连接在settings.py
中设置:
'database_oracle' : {
'ENGINE' : 'oracle',
'NAME' : 'name',
'USER' : 'user',
'PASSWORD' : 'pw',
'HOST' : 'connection',
'PORT' : 1234,
'SCHMEA' : 'abc',
},
我的models.py
中的模型还有其他元类:
class Meta:
db_table = '"abc.table1"'
data_source = 'database_oracle'
data_source_app = 'myapp'
问题是
Model1.objects.all()
返回错误:`
django.db.utils.DatabaseError:ORA-00942:表或视图不存在
但是如果我使用django.db
连接:
from django.db import connections
db_conn = connections['database_oracle']
c = db_conn.cursor()
c.execute("SELECT MAX(date) FROM abc.table1 WHERE key = 1234567").fetchall()
效果很好。 所以我尝试了db_table的不同拼写:
db_table ='table1'
db_table ='“ table1”'
db_table ='abc.table1'
db_table ='“ abc.table1”'
每次都会迁移更改,这就是我的routers.py
的样子(if语句属于其他应用程序和其他数据库):
myapps = ['myapp']
class DataRouter:
def db_for_read(self, model, **hints):
if [...]
elif model._meta.app_label in myapps:
return 'database_oracle'
return None
def db_for_write(self,model, **hints):
if [...]
elif model._meta.app_label in myapps:
return 'database_oracle'
return None
def allow_relation(self, obj1, obj2, **hints):
if [...]
elif obj1._meta.app_label in myapps or obj2._meta.app_label in myapps:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if [...]
elif app_label in myapps:
return db =='database_oracle'
return None
有人知道为什么模型无法连接到我的数据库吗?