"Where did you get the idea that `force_update` would allow you to
change the primary key of an existing object? That's not implied by
the documentation, and certainly isn't supported in the code."

While it is not implied that a primary key cannot be changed, it is
likewise not implied or stated that a primary key cannot be changed or
updated. Without any contrary indications I assumed that all
attributes are considered equal. What I did not realize is that an
object does not cache the pk and therefore the breadcrumbs leading
home. However, a queryset apparently does.

In any case, the queryset approach worked perfectly. I appreciate your
explanation and solution.

Thanks!
 -r

On Sep 15, 10:02 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Sep 14, 8:44 pm, Anomal <rsprin...@gmail.com> wrote:
>
>
>
> > According to the Django docs,  a new row is created with the new value
> > primary key when  when I change the value of a primary key and save
> > the object.
>
> > In this case, I am looking to change the value. So, I use
> > force_update=True. Instead of changing the value of the primary key, I
> > get a DatabaseError. Any ideas of what I am doing incorrectly?
>
> > class BuildState(models.Model):
> >     build_state = models.CharField(primary_key=True, max_length=32)
>
> >     class Meta:
> >         db_table = u'buildstate'
>
> >     def __unicode__(self):
> >         return  "BuildState{'build_state' : '%s'}" %
> > (self.build_state)
>
> > >>> from orm.models import BuildState
> > >>> foo = BuildState.objects.get(build_state='failed')
> > >>> foo
>
> > <BuildState: BuildState{'build_state' : 'failed'}>>>> 
> > foo.build_state='nirvana'
> > >>> foo.save(force_update=True)
>
> > Traceback (most recent call last):
> >   File "<console>", line 1, in <module>
> >   File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
> > 410, in save
> >     self.save_base(force_insert=force_insert,
> > force_update=force_update)
> >   File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line
> > 476, in save_base
> >     raise DatabaseError("Forced update did not affect any rows.")
> > DatabaseError: Forced update did not affect any rows.
>
> > >>> foo.save()
> > >>> # This works as expected. A new row is added.
>
> > Thanks,
> >  -Rick
>
> Where did you get the idea that `force_update` would allow you to
> change the primary key of an existing object? That's not implied by
> the documentation, and certainly isn't supported in the code.
>
> Once you've changed the contents of the field that represents the
> primary key, Django has no way of knowing that the object you're
> holding is the "same" as the one in the database with the old PK. So
> no combination of parameters to `save` will accomplish what you want.
>
> You could do it with `update`:
>
> BuildState.objects.filter(build_state='failed').update(build_state='nirvana')
>
> This has the advantage that it's a single database call, although the
> disadvantage that you don't actually get the object at any point, so
> you'd need another `get()` call to do so if you need it.
> --
> DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to