Kaydet (Commit) 63ba472c authored tarafından Karen Tracey's avatar Karen Tracey

Fix #13864: Removed database error raised when force_update is requsted on save…

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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17088 bcc190cf-cafb-0310-a4f2-bffc1f526a37
üst eb81f979
...@@ -87,6 +87,7 @@ answer newbie questions, and generally made Django that much better: ...@@ -87,6 +87,7 @@ answer newbie questions, and generally made Django that much better:
Julian Bez Julian Bez
Arvis Bickovskis <viestards.lists@gmail.com> Arvis Bickovskis <viestards.lists@gmail.com>
Natalia Bidart <nataliabidart@gmail.com> Natalia Bidart <nataliabidart@gmail.com>
Mark Biggers <biggers@utsl.com>
Paul Bissex <http://e-scribe.com/> Paul Bissex <http://e-scribe.com/>
Simon Blanchard Simon Blanchard
David Blewett <david@dawninglight.net> David Blewett <david@dawninglight.net>
......
...@@ -526,9 +526,10 @@ class Model(object): ...@@ -526,9 +526,10 @@ class Model(object):
# It does already exist, so do an UPDATE. # It does already exist, so do an UPDATE.
if force_update or non_pks: if force_update or non_pks:
values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks] values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
rows = manager.using(using).filter(pk=pk_val)._update(values) if values:
if force_update and not rows: rows = manager.using(using).filter(pk=pk_val)._update(values)
raise DatabaseError("Forced update did not affect any rows.") if force_update and not rows:
raise DatabaseError("Forced update did not affect any rows.")
else: else:
record_exists = False record_exists = False
if not pk_set or not record_exists: if not pk_set or not record_exists:
......
...@@ -9,6 +9,16 @@ class Counter(models.Model): ...@@ -9,6 +9,16 @@ class Counter(models.Model):
name = models.CharField(max_length = 10) name = models.CharField(max_length = 10)
value = models.IntegerField() value = models.IntegerField()
class InheritedCounter(Counter):
tag = models.CharField(max_length=10)
class ProxyCounter(Counter):
class Meta:
proxy = True
class SubCounter(Counter):
pass
class WithCustomPK(models.Model): class WithCustomPK(models.Model):
name = models.IntegerField(primary_key=True) name = models.IntegerField(primary_key=True)
value = models.IntegerField() value = models.IntegerField()
...@@ -3,14 +3,15 @@ from __future__ import absolute_import ...@@ -3,14 +3,15 @@ from __future__ import absolute_import
from django.db import transaction, IntegrityError, DatabaseError from django.db import transaction, IntegrityError, DatabaseError
from django.test import TestCase from django.test import TestCase
from .models import Counter, WithCustomPK from .models import (Counter, WithCustomPK, InheritedCounter, ProxyCounter,
SubCounter)
class ForceTests(TestCase): class ForceTests(TestCase):
def test_force_update(self): def test_force_update(self):
c = Counter.objects.create(name="one", value=1) c = Counter.objects.create(name="one", value=1)
# The normal case
# The normal case
c.value = 2 c.value = 2
c.save() c.save()
# Same thing, via an update # Same thing, via an update
...@@ -38,3 +39,25 @@ class ForceTests(TestCase): ...@@ -38,3 +39,25 @@ class ForceTests(TestCase):
# the data isn't in the database already. # the data isn't in the database already.
obj = WithCustomPK(name=1, value=1) obj = WithCustomPK(name=1, value=1)
self.assertRaises(DatabaseError, obj.save, force_update=True) self.assertRaises(DatabaseError, obj.save, force_update=True)
class InheritanceTests(TestCase):
def test_force_update_on_inherited_model(self):
a = InheritedCounter(name="count", value=1, tag="spam")
a.save()
a.save(force_update=True)
def test_force_update_on_proxy_model(self):
a = ProxyCounter(name="count", value=1)
a.save()
a.save(force_update=True)
def test_force_update_on_inherited_model_without_fields(self):
'''
Issue 13864: force_update fails on subclassed models, if they don't
specify custom fields.
'''
a = SubCounter(name="count", value=1)
a.save()
a.value = 2
a.save(force_update=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment