Opened 15 years ago

Closed 15 years ago

Last modified 12 years ago

#10205 closed (fixed)

Update on a QuerySet with filter of field__in='' causes AttributeError

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: rico.bl@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alex Gaynor)

Let testfield be models.CharField

>>> TestModel.objects.filter(testfield__in='')
[]
>>> TestModel.objects.filter(testfield__in=())
[]
>>> TestModel.objects.filter(testfield__in=()).delete()

>>> TestModel.objects.filter(testfield__in='non-existent-entry').update(testfield='test')
0
>>> TestModel.objects.filter(testfield__in='').update(testfield='test')

as well as

>>> TestModel.objects.filter(testfield__in=()).update(testfield='test')

causes following trackback:

'NoneType' object has no attribute 'rowcount'
File "C:\Python25\lib\site-packages\django\db\models\query.py" in update
  448.         rows = query.execute_sql(None)
File "C:\Python25\lib\site-packages\django\db\models\sql\subqueries.py" in execute_sql
  120.         rows = cursor.rowcount

Attachments (1)

subqueries.py.diff (569 bytes ) - added by Enrico 15 years ago.
Patch to execute_sql in django/db/models/sql/subqueries.py

Download all attachments as: .zip

Change History (9)

comment:1 by Alex Gaynor, 15 years ago

Description: modified (diff)

cleaned up formatting.

by Enrico, 15 years ago

Attachment: subqueries.py.diff added

Patch to execute_sql in django/db/models/sql/subqueries.py

comment:2 by Enrico, 15 years ago

Cc: rico.bl@… added
Has patch: set

I've created a patch over r9904 to fix this issue.

If someone needs a quick fix without patching django, here's an ugly workaround:

try:
    TestModel.objects.filter(testfield__in=()).update(testfield='test')
except AttributeError, e:
    if not e.message.find('rowcount') > 0:
        raise

comment:3 by Malcolm Tredinnick, 15 years ago

Which database backend has this problem? It might be caused by a bug in the DB API wrapper, since the rowcount attribute is meant to exist always (per PEP 249). At a minimum, I'd like to comment what bug we're working around (if it's something current), but also we should consider reporting upstream.

comment:4 by Enrico, 15 years ago

I've got this error with MySQL (mysql Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (i486) using readline 5.2)

The database gets the update but the exception is raised.

comment:5 by (none), 15 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:6 by Jacob, 15 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:7 by Malcolm Tredinnick, 15 years ago

Resolution: fixed
Status: newclosed

(In [9926]) Fixed insert/update handling when no database interaction is required.

Fixed #10205 as part of this.

comment:8 by Jacob, 12 years ago

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top