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)
Change History (13)
comment:1 by , 14 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 14 years ago
Resolution: | invalid |
---|---|
Status: | closed → 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. >>>
by , 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 , 14 years ago
Has patch: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 1.2 → 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.
by , 14 years ago
Attachment: | 13864.tests_and_possible_fix.patch added |
---|
Patch with tests and a possible, simple fix.
comment:5 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:6 by , 14 years ago
Easy pickings: | unset |
---|---|
Patch needs improvement: | set |
13864.tests_and_possible_fix.patch fails to apply cleanly on to trunk
comment:7 by , 13 years ago
Patch needs improvement: | unset |
---|
by , 13 years ago
Attachment: | r17083.diff added |
---|
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.