Opened 6 years ago

Closed 6 years ago

Last modified 4 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: rico.bl@… 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)

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 rico 6 years ago.
Patch to execute_sql in django/db/models/sql/subqueries.py

Download all attachments as: .zip

Change History (9)

comment:1 Changed 6 years ago by Alex

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

cleaned up formatting.

Changed 6 years ago by rico

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

comment:2 Changed 6 years ago by rico

  • 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 Changed 6 years ago by mtredinnick

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 6 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 6 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:6 Changed 6 years ago by jacob

  • milestone set to 1.1
  • Triage Stage changed from Unreviewed to Accepted

comment:7 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

Fixed #10205 as part of this.

comment:8 Changed 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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