#12634: sql compiler execute: fetchone empty result with ordering_aliases fail.
---------------------------------------------------+------------------------
Reporter: [email protected] | Owner: nobody
Status: closed | Milestone:
Component: Database layer (models, ORM) | Version: 1.2-beta
Resolution: invalid | Keywords:
Stage: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Comment (by PyMan):
I am not that sure it's just a django-pyodbc problem, maybe it uses the
self.query_ordering_aliases in a way slightly different from other
backends but it gives back a correct query to be executed by django (and
so it is) and problem happens when the query gives no results.
Coming back to my previous example I could see that even doing this the
problem happens:
{{{
Test.objects.create(name="pippo", id=12345) #At least when 12345 doesn't
exists
}}}
That is it happens when an explicit id is given (because now it's doing a
select, not an insert!)
So django/db/models/sql/compiler.py with the "execute_sql" function
executes the query (the following query)
{{{
SELECT * FROM (
SELECT (1) AS [a], (ROW_NUMBER() OVER (ORDER BY RAND() )) AS [rn]
FROM [myapp_test]
WHERE [myapp_test].[id] = %s
) AS X
WHERE X.rn BETWEEN 1 AND 1
Query Parameters: (12345,)
}}}
When this happens the self.query.ordering_aliases has the value
"['(ROW_NUMBER() OVER (ORDER BY RAND() )) AS [rn]']"
After executing the query the same function executes this piece of code
{{{
if result_type == SINGLE:
if self.query.ordering_aliases:
#res = cursor.fetchone()
#if res : return res[:-len(self.query.ordering_aliases)]
#return res
return
cursor.fetchone()[:-len(self.query.ordering_aliases)] #HERE'S THE
EXCEPTION!
return cursor.fetchone()
}}}
The exception comes because cursor.fetchone() returns None at least when
the "id" doesn't exists in the table. I added the commented lines that if
used things work.
Can this help?
--
Ticket URL: <http://code.djangoproject.com/ticket/12634#comment:12>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.