Django inspectdb 使用 Oracle 数据库的问题
我安装了cx_oracle,然后运行了inspectdb。好像没有任何输出?有人能帮忙吗?使用inspectdb和Oracle之间有没有已知的问题?
下面是我使用的命令和settings.py的内容。
python manage.py inspectdb --database xxx_db
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# Feel free to rename the models, but don't rename db_table values or field names.
#
# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'
# into your database.
from django.db import models
settings.py
DATABASES = {
'xxx_db': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'abc',
'USER': 'abc_read',
'PASSWORD': 'abc_read',
'HOST': 'apps.domain.com',
'PORT': 'xxxx'
},
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'aaaa',
'USER': 'aaaa',
'PASSWORD': 'xxxx',
'HOST': '/tmp/mysql.sock',
'PORT': ''
}
}
5 个回答
0
@Plecebo说得对。问题出在get_table_list这个方法上,但给出的Select语句并没有奏效。
为了快速得到我需要的东西,我暂时把表名写死了:
(django/db/backends/oracle/introspection.py 第40行)
def get_table_list(self, cursor):
"Returns a list of table names in the current database."
#cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")
return ['table_name1', 'table_name2']
#return [row[0].lower() for row in cursor.fetchall()]
0
对我来说是可以的。
你有没有检查过用户是否有权限查看Oracle里的所有表?
不过我很好奇inspectdb使用的是什么SQL语句。
1
有两件事:
- inspectdb这个功能官方并不支持Oracle数据库(可以查看Django文档 - inspectdb),这让人有点失望。
- Django对Oracle数据库的支持不是特别强(可以查看未解决的Django问题 6148),所以如果你能用该模式的主用户连接,可能会更顺利,这样你想要查看的模式就可以设为默认模式。
我通过修改introspection.py里的选择语句,成功得到了一个基本的模型文件输出。对我来说,我把django/db/backends/oracle/introspection.py文件中大约第40行的get_table_list函数改成了:
def get_table_list(self, cursor):
"Returns a list of table names in the current database."
cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")
return [row[0].lower() for row in cursor.fetchall()]
然后改成了:
def get_table_list(self, cursor):
"Returns a list of table names in the current database."
cursor.execute("SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = 'SCHEMA_TO_QUERY'")
return [row[0].lower() for row in cursor.fetchall()]
但当我看到Django对Oracle模式的整体支持很差时,我就放弃了使用Django。