Doing a save after primary key has changed should raise an error
|Reported by:||akaariai||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.3|
|Cc:||akaariai||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 3 years ago by akaariai
- Cc akaariai added
- Has patch set
- Needs documentation set
- Needs tests set
- Patch needs improvement unset
Changed 3 years ago by akaariai
comment:2 Changed 3 years ago by lukeplant
- Triage Stage changed from Unreviewed to Design decision needed
comment:3 Changed 3 years ago by akaariai
- Resolution set to wontfix
- Status changed from new to closed