#4186 closed (wontfix)
[boulder-oracle] Error using "SELECT DISTINCT" with TextFields
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | oracle distinct NCLOB TextField | |
Cc: | Erin Kelly, Matt Boersma, Justin | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
I believe I have stumbled across a bug with the boulder-oracle-sprint branch. The issue is related to the use of the "DISTINCT" keyword and use of django TextFields. Behind the scenes the TextField is mapped to the NCLOB data type. The following example will cause the error to occur.
Using the following model,
from django.db import models from django.contrib.auth.models import * # Create your models here. class MyUser(models.Model): basenode = models.OneToOneField(User) life_story = models.TextField(blank=True)
Enter the following into the shell
In [1]: from django.contrib.auth.models import * In [2]: from testapp.models import * In [3]: In [3]: user = User.objects.create_user(username='Ben',email='ben@isp.com',password='password') In [4]: user.save() In [5]: In [5]: my_user = MyUser(basenode=user) In [6]: my_user.save() In [7]: In [7]: MyUser.objects.filter(basenode__username='Ben') Out[7]: [<MyUser: MyUser object>] In [8]: MyUser.objects.filter(basenode__username='Ben').distinct()
The last line will produce a long exception ending with this error
DatabaseError: ORA-00932: inconsistent datatypes: expected - got NCLOB
Further digging into this has shown that the problem can be replicated my entering the query manually into an Oracle console.
The following query will produce the same error
SELECT DISTINCT "TESTAPP_MYUSER"."BASENODE_ID", "TESTAPP_MYUSER"."LIFE_STORY" FROM "TESTAPP_MYUSER" INNER JOIN "AUTH_USER" "TESTAPP_MYUSER__BASENODE" ON "TESTAPP_MYUSER"."BASENODE_ID" = "TESTAPP_MYUSER__BASENODE"."ID" WHERE ("TESTAPP_MYUSER__BASENODE"."USERNAME" = Ben);
Removing "DISTINCT" or "TESTAPP_MYUSER.LIFE_STORY" from the query will allow the query to execute successfully.
My suspicion is that there is some restriction regarding the use of NCLOB datatypes and the DISTINCT keyword.
Regards
Ben Khoo
Change History (13)
comment:1 by , 18 years ago
Summary: | Error using "SELECT DISTINCT" with TextFields → [boulder-oracle] Error using "SELECT DISTINCT" with TextFields |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 18 years ago
comment:3 by , 18 years ago
I am getting this same exact Oracle error whenever I try to view the history of an object on the administration site. This makes the administration site much less useful.
Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/django/template/init.py" in render_node
- result = node.render(context)
File "/usr/lib/python2.5/site-packages/django/template/defaulttags.py" in render
- if (value and not ifnot) or (ifnot and not value):
File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in len
- return len(self._get_data())
File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in _get_data
- self._result_cache = list(self.iterator())
File "/usr/lib/python2.5/site-packages/django/db/backends/oracle/base.py" in iterator
- cursor.execute(full_query, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/util.py" in execute
- return self.cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/oracle/base.py" in execute
- return Database.Cursor.execute(self, query, params)
DatabaseError at /admin/polls/poll/1/history/
ORA-00932: inconsistent datatypes: expected - got NCLOB
comment:4 by , 18 years ago
The error I am getting is probably because the poll model has a TextField field.
"LOB columns may not be used in a "SELECT DISTINCT" list. This means that attempting to use the QuerySet.distinct method on a model that includes TextField columns will result in an error. A workaround to this is to keep TextFields out of any models that you foresee performing .distinct queries on, and to include the TextFields in a related model instead."
http://code.djangoproject.com/wiki/OracleBranch
Is there no possible way to resolve this? We must separate our models into unnatural formats?
comment:5 by , 17 years ago
I've submitted a patch for the latter bug (using a TextField in a WHERE clause) in #5087. I still don't think there's any real solution for the original bug (using SELECT DISTINCT with a TextField).
comment:6 by , 17 years ago
Version: | other branch → SVN |
---|
comment:7 by , 17 years ago
Cc: | added |
---|
comment:8 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:9 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:10 by , 16 years ago
Cc: | added |
---|
comment:11 by , 15 years ago
Cc: | added |
---|
comment:12 by , 14 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
This ticket is long overdue to be closed wontfix, so I'm doing that now. There seems to be no way to fix the core bug. Moreover, this error is easy to work around in Django 1.1+ by using the QuerySet.defer method to keep the TextField out of the select distinct list.
This is a limitation of Oracle. I added some documentation to the OracleBranch page describing the relevant limitations that Oracle places on LOB columns.