#2259: PK Change creates new object instead of update
---------------------------------------------------+------------------------
Reporter: [email protected] | Owner: nobody
Status: reopened | Milestone:
Component: Database layer (models, ORM) | Version:
Resolution: | Keywords:
Stage: Design decision needed | Has_patch: 0
Needs_docs: 1 | Needs_tests: 0
Needs_better_patch: 1 |
---------------------------------------------------+------------------------
Comment (by [email protected]):
I think this issue is actually illustrating a greater flaw in the overall
transactional structure the Django ORM implements.
Right now a save transaction works like this:
obj.save
transaction>
SELECT obj
if exists: UPDATE
else: INSERT
<transaction
There are several issues with this. In a write heave database this will
likely lead to serious performance issues. If an object gets deleted,
there is the chance in a race-condition that it will be re-inserted... and
obviously you can't change the primary key, because the whole transaction
works based off of whether an object exists with the same PK.
How it should work:
obj = MyModel.objects.get(id = 1) # there is a private variable set with
the original PK
obj.field = 1
obj.save() # because there is
the original PK set, it does an update
So how it works here is based off of private variables. Going beyond
having a PK that is private, you could also have the rest of the fields
have non-mutable private variables to generate cleaner, shorter SQL
statements ... though I'm not sure that would have any real benefit.
There should only be inserts on new objects...
obj = MyModel()
... and there is absolutely no reason to do a SELECT at the start of the
transactional block. In write heavy applications, this will be horrible
for performance.
--
Ticket URL: <http://code.djangoproject.com/ticket/2259#comment:15>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.