Opened 14 years ago

Closed 13 years ago

#13864 closed Bug (fixed)

DatabaseError while saving with force_update set

Reported by: fva Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: force_update DatabaseError
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Models:

class A ( models.Model ) :
    a = models.IntegerField ( null=True )

class B ( A ) : pass

session:

In [3]: b = B()

In [4]: b.save()

In [5]: b.save ( force_update = True )
---------------------------------------------------------------------------
DatabaseError                             Traceback (most recent call last)

<ipython console> in <module>()

django/db/models/base.pyc in save(self, force_insert, force_update, using)
    428         if force_insert and force_update:
    429             raise ValueError("Cannot force both insert and updating in model saving.")
--> 430         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
    431 
    432     save.alters_data = True

django/db/models/base.pyc in save_base(self, raw, cls, origin, force_insert, force_update, using)
    496                         rows = manager.using(using).filter(pk=pk_val)._update(values)
    497                         if force_update and not rows:
--> 498                             raise DatabaseError("Forced update did not affect any rows.")
    499                 else:
    500                     record_exists = False

DatabaseError: Forced update did not affect any rows.

but simple b.save() updates fine.

Attachments (4)

13864.tests.patch (1.3 KB ) - added by Gregor Müllegger 14 years ago.
First tests that fail for the inherited model that is no proxy and has no custom fields specified.
13864.tests_and_possible_fix.patch (2.6 KB ) - added by Gregor Müllegger 14 years ago.
Patch with tests and a possible, simple fix.
13864-r16288.diff (3.2 KB ) - added by Ramiro Morales 14 years ago.
Patch updated to r16288.
r17083.diff (3.4 KB ) - added by markb1 13 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 by Alex Gaynor, 14 years ago

Resolution: invalid
Status: newclosed

That behavior looks correct. An unsaved model (one with no pk) can't be updated. Pretty much don't use the force_insert/force_update if you don't have a good reason.

comment:2 by Karen Tracey, 14 years ago

Resolution: invalid
Status: closedreopened

The model was saved, in step 4. It does have a PK:

>>> from ttt.models import B
>>> b = B()
>>> b.pk
>>> b.a
>>> b.save()
>>> b.pk
2
>>> b.a
>>> b.save(force_update=True)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\u\kmt\django\trunk\django\db\models\base.py", line 435, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "C:\u\kmt\django\trunk\django\db\models\base.py", line 503, in save_base
    raise DatabaseError("Forced update did not affect any rows.")
DatabaseError: Forced update did not affect any rows.
>>>

comment:3 by Alex Gaynor, 14 years ago

Doh, my apologies.

by Gregor Müllegger, 14 years ago

Attachment: 13864.tests.patch added

First tests that fail for the inherited model that is no proxy and has no custom fields specified.

comment:4 by Gregor Müllegger, 14 years ago

Has patch: set
Triage Stage: UnreviewedAccepted
Version: 1.2SVN

The problem is that the subclass has no custom fields assigned. So the subclassed model will try to make an update query without and values this results in an empty query.

There is a very simple fix for this, added in the second patch. This avoids making the empty query that ofcourse doesn't affect any rows which causes the error.

Maybe someone with a better understanding of the ORM could have a look if this fix is sufficient and confirm that it doesn't have sideeffects to NOT execute the update query if there is nothing to change.

by Gregor Müllegger, 14 years ago

Patch with tests and a possible, simple fix.

comment:5 by Graham King, 14 years ago

Severity: Normal
Type: Bug

comment:6 by patchhammer, 14 years ago

Easy pickings: unset
Patch needs improvement: set

13864.tests_and_possible_fix.patch fails to apply cleanly on to trunk

by Ramiro Morales, 14 years ago

Attachment: 13864-r16288.diff added

Patch updated to r16288.

comment:7 by Ramiro Morales, 14 years ago

Patch needs improvement: unset

comment:8 by markb1, 13 years ago

UI/UX: unset

Patch updated to r17083. All relevant tests pass.

by markb1, 13 years ago

Attachment: r17083.diff added

comment:9 by Karen Tracey, 13 years ago

Resolution: fixed
Status: reopenedclosed

In [17088]:

Fix #13864: Removed database error raised when force_update is requsted on save of an inherited model with no fields of its own. Thanks fva, gregmuellegger, and markb1.

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