ORA-01861: 在 django 中执行获取模型对象时文字与格式字符串不匹配
Posted
技术标签:
【中文标题】ORA-01861: 在 django 中执行获取模型对象时文字与格式字符串不匹配【英文标题】:ORA-01861: literal does not match format string when executing get model object in django 【发布时间】:2015-03-10 21:18:34 【问题描述】:我在 django 中有一个模型对象,就像这样......
from django.db import models
class Person(models.Model):
employee_title = models.CharField(max_length=150)
pk_person_id = models.IntegerField(primary_key=True)
department_name = models.CharField(max_length=240)
cost_center = models.CharField(max_length=150)
user_name = models.CharField(max_length=100)
def __str__(self):
return self.user_name
class Meta:
managed = False
db_table = 'company_hr_idm_data_v'
在我看来,我正在像这样对 Person 对象进行简单的获取...
testobj = Person.objects.using('oracle').get(pk=4)
代码错误并带有DatabaseError
,上面写着
ORA-01861: 文字与格式字符串不匹配
这是我在 settings.py 中将调试模式设置为 true 时编写的堆栈跟踪
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/roles/testing
Django Version: 1.7.2
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'roles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/adjeibad/.virtualenvs/wwtmlserver/entitlements/roles/views/Organization.py" in test
9. testobj = Person.objects.using('oracle').get(pk=4)
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site- packages/django/db/models/query.py" in get
351. num = len(clone)
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/db/models/query.py" in __len__
122. self._fetch_all()
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
966. self._result_cache = list(self.iterator())
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/db/models/query.py" in iterator
265. for row in compiler.results_iter():
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
700. for rows in self.execute_sql(MULTI):
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
786. cursor.execute(sql, params)
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
81. return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/Users/adjeibad/.virtualenvs/wwtmlserver/lib/python2.7/site- packages/django/db/backends/oracle/base.py" in execute
916. return self.cursor.execute(query, self._param_generator(params))
Exception Type: DatabaseError at /roles/testing
Exception Value: ORA-01861: literal does not match format string
哦!这里也是生成的查询...
SELECT * FROM (SELECT ROWNUM AS "_RN", "_SUB".* FROM (SELECT "COMPANY_HR_IDM_DATA_V"."EMPLOYEE_TITLE", "COMPANY_HR_IDM_DATA_V"."PK_PERSON_ID", "COMPANY_HR_IDM_DATA_V"."DEPARTMENT_NAME", "COMPANY_HR_IDM_DATA_V"."COST_CENTER", "COMPANY_HR_IDM_DATA_V"."USER_NAME" FROM "COMPANY_HR_IDM_DATA_V" WHERE "COMPANY_HR_IDM_DATA_V"."PK_PERSON_ID" = :arg0) "_SUB" WHERE ROWNUM <= 21) WHERE "_RN" > 0
我试图提供尽可能多的信息,谁能引导我朝着正确的方向前进?
【问题讨论】:
在我检索对象之前需要执行的语句吗?票说他们在处理日期时遇到了问题。我没有访问日期信息。我确实注意到 django 会运行他们在应用启动时提到的这两个查询。 你说得对,看起来这张票实际上并不相关。尽管it's possible this is a bug in Django,但我已删除该评论以防止将来出现混淆。 可能是db中的表已经创建,而不是模型同步 表已经创建。我要做的就是从中读取数据。我已经尝试为数据库上的另一个表创建一个模型,它工作得很好。当我尝试访问 company_hr_idm_data_v 时,它给了我该错误,并且我使用的 db 用户具有完全访问权限 那么可能表列 pk_person_id 是为字符串或其他东西而不是整数创建的,所以当传递 pk=4 时,它是整数,但预期是字符串 【参考方案1】:您可以尝试通过输入直接执行查询吗?输入和实际列类型之间可能存在数据类型差异。
【讨论】:
以上是关于ORA-01861: 在 django 中执行获取模型对象时文字与格式字符串不匹配的主要内容,如果未能解决你的问题,请参考以下文章
java sql SQLException ORA-01861 文字和格式字符串不匹配