ORA-01861: literal does not match format string when executing get model object in django
Asked Answered
P

1

8

I have a model object in django like so...

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'

and in my view I am doing a simple get on the Person object like so...

testobj = Person.objects.using('oracle').get(pk=4)

The code errors out with a DatabaseError that says

ORA-01861: literal does not match format string

Here is the stack trace I code when I have my debug mode set to true in my settings.py

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

Oh! also here is the query that is generated...

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

I tried to put as much information as I could, can anyone please steer me in the right direction?

Prey answered 13/1, 2015 at 0:49 Comment(9)
Does that need to be a statement that needs to be execute before my retrieval of the object? The tickets says that they encountered the problem when working with dates. I am not accessing date information. I do notice that django runs those two queries they mention on app start up.Prey
You're right, it looks like that ticket isn't actually related. I've deleted that comment to prevent future confusion, though it's possible this is a bug in Django.Cuddy
could it be that the table in db was created already and not by the model syncSheers
The table was created already. All I am trying to do is to read data from it. I have tried creating a model for another table on the database and it works just fine. When I try an access company_hr_idm_data_v it gives me that error and the db user I am using has full access rightsPrey
Then probably the table column pk_person_id is created for string or something but not integer, so when pk=4 is passed which is integer but what is expected is to be stringSheers
I have all my types matching up to the data types in the database. Even the lengths. The funny thing is I did a Person.objects.using('oracle').all() and it gives me the exact same errorPrey
If you run the sql by itself does it generate an error?Tarver
Did you try python manage.py inspectdb to create a model for the table, and then try accessing the table using this new model?Tarver
Can you provide the output of 'desc company_hr_idm_data_v;' from the SQL Plus prompt or Toad or some other SQL shell? Alternatively, the result of 'select owner, table_name, column_name, data_type, data_type_mod, data_length, data_precision, data_scale, nullable from all_tab_columns where table_name = 'company_hr_idm_data_v';'Myriad
C
0

Can you try executing the Query directly by giving the input?. There might be a datatype difference between the input and the actual column type.

Conferva answered 27/2, 2015 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.