Doing a save after primary key has changed should raise an error
|Reported by:||Anssi Kääriäinen||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.3|
|Cc:||Anssi Kääriäinen||Triage Stage:||Design decision needed|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||yes||Patch needs improvement:||no|
Currently, if you change primary key and do a save(), you will get a duplicate in the database. In effect, the current behavior of save is: "save the object to DB, except when PK has changed, then do a clone". This API is not good for natural primary keys. Natural primary keys aren't that common currently, but if multicolumn primary keys are introduced into Django, they will be more common.
The idea is to deprecate the current clone behavior of save raise an error instead. This would need to go through the normal deprecation cycle. You can get the current behavior back by using .save(clone=True). For
AutoFields, setting the pk to None will work.
For implementation the problems are: How to trac primary key changes? Some fields are mutable inplace, which makes the problem harder. Model
__init__ should not be made much slower by this.
Change History (5)
comment:1 Changed 5 years ago by
|Cc:||Anssi Kääriäinen added|
|Patch needs improvement:||unset|