Models inherited from multiple Models
|Reported by:||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.1|
|Cc:||bas@…, shaunc@…||Triage Stage:||Accepted|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
In case Model is created as inherited from multiple non-abstract Models there is a problem with automatic primary keys at children model save. Especially when second parent has one more non-proxy children.
Assume we have following models:
class Place(models.Model): class Meta: abstract = False proxy = False ... class Staff(models.Model): class Meta: abstract = False proxy = False ... class Restaurant(Place, Staff): class Meta: abstract = False proxy = False ... class OtherStaff(Staff): class Meta: abstract = False proxy = False ...
where Place and Staff has automatic primary keys:
id = models.AutoField(primary_key = True)
Then on saving new instance of Restaurant the following happens:
- Restaurant searches for its parents and finds out Place and Staff
- While inserting new Place instance, it comes out that it has no primary key ("id"), so it is inserted into database with automatic one and primary key is returned
- Discovered primary key of new Place instance is saved to Restaurant instance under attribute name of Place primary key ("id")
- Then new Staff instance is inserted. It looks whether it has a primary key and it finds out primary key of Place instance, because it has same attribute name for primary key ("id")
- New Staff instance is saved with primary key of Place instance instead its own (Hidden problem: If Staff with same primary key already exists then values of that Staff are updated with values from new Restaurant instance)
Following attributes will always be equal: Restaurant.id, Restaurant.place_ptr_id, Restaurant.staff_ptr_id, Place.id, Staff.id
Definitely crashes in case Staff has another children e.g. OtherStaff, then on saving new instance of OtherStaff:
- OtherStaff searches for its parents and finds out Staff
- While inserting new Staff instance, it comes out that it has no primary key ("id"), so it is inserted into database with automatic one
- In database a next primary key from sequence is found out and Staff is supposed to be inserted with that primary key
- CRASH because Staff with that primary key already exist (created while saving Restaurant, but with specified primary key, so sequence was not updated)
Same situation happened in every case Model parents (non-abstract) has same attribute name for their primary keys and second one has at least one more non-proxy children Model.
I consider this as a bug although it has easy solution (rename attribute name of primary key), because I found no evidence that this situation can happen and therefore it should be considered. Also I see no reason for use in current state.
I my opinion the best (and easiest) solution is Exception raised in case parent classes has same attribute name for their primary key. A way that will solve this entirely and allowed situation that parents can have same attribute name of primary keys would be great, but it would be probably much too complicated.