Opened 9 years ago

Closed 9 years ago

Last modified 6 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: master
Severity: Keywords:
Cc:… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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')
>>> 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\" in update
  448.         rows = query.execute_sql(None)
File "C:\Python25\lib\site-packages\django\db\models\sql\" in execute_sql
  120.         rows = cursor.rowcount

Attachments (1) (569 bytes) - added by rico 9 years ago.
Patch to execute_sql in django/db/models/sql/

Download all attachments as: .zip

Change History (9)

comment:1 Changed 9 years ago by Alex Gaynor

Description: modified (diff)

cleaned up formatting.

Changed 9 years ago by rico

Attachment: added

Patch to execute_sql in django/db/models/sql/

comment:2 Changed 9 years ago by rico

Cc:… 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:

except AttributeError, e:
    if not e.message.find('rowcount') > 0:

comment:3 Changed 9 years ago by Malcolm Tredinnick

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 Changed 9 years ago by rico

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 Changed 9 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:6 Changed 9 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:7 Changed 9 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

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

Fixed #10205 as part of this.

comment:8 Changed 6 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

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