Opened 5 years ago

Closed 3 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: master
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 gregmuellegger 5 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 gregmuellegger 5 years ago.
Patch with tests and a possible, simple fix.
13864-r16288.diff (3.2 KB) - added by ramiro 4 years ago.
Patch updated to r16288.
r17083.diff (3.4 KB) - added by markb1 3 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 Changed 5 years ago by kmtracey

  • Resolution invalid deleted
  • Status changed from closed to reopened

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 Changed 5 years ago by Alex

Doh, my apologies.

Changed 5 years ago by gregmuellegger

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

comment:4 Changed 5 years ago by gregmuellegger

  • Has patch set
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.2 to SVN

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.

Changed 5 years ago by gregmuellegger

Patch with tests and a possible, simple fix.

comment:5 Changed 4 years ago by graham_king

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 4 years ago by patchhammer

  • Easy pickings unset
  • Patch needs improvement set

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

Changed 4 years ago by ramiro

Patch updated to r16288.

comment:7 Changed 4 years ago by ramiro

  • Patch needs improvement unset

comment:8 Changed 3 years ago by markb1

  • UI/UX unset

Patch updated to r17083. All relevant tests pass.

Changed 3 years ago by markb1

comment:9 Changed 3 years ago by kmtracey

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

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