Opened 7 years ago
Closed 5 years ago
#29129 closed Bug (fixed)
Child model updates parent model with empty fields making an extra query in multi-inheritance when parent model has custom PK
Reported by: | user0007 | Owned by: | Abhijeet Viswa |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Simon Charette, Abhijeet Viswa | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
While creating a new model object (using multi-inheritance model => Child(Parent)
), Django does an extra update query setting parent model fields to empty values. This situation occurs *only* if we define a custom primary key in a parent model (eg. as an UUID field).
An example *without* custom primary key (correct behavior):
class Parent(models.Model): title = models.TextField() class Child(Parent): body = models.TextField() >> Child.objects.create() 1. INSERT INTO "app_parent" ("title") VALUES ('') RETURNING "app_parent"."id" 2. INSERT INTO "app_child" ("parent_ptr_id", "body") VALUES (1, '')
An example *with* custom primary key (incorrect behavior):
class Parent(models.Model): id = models.UUIDField( primary_key=True, default=uuid.uuid4, editable=False ) title = models.TextField() class Child(Parent): body = models.TextField() >> Child.objects.create() 1. UPDATE "app_parent" SET "title" = '' WHERE "app_parent"."id" = 'd750cfdd-ae7b-48a6-a2e0-d49e70e28686'::uuid 2. INSERT INTO "app_parent" ("id", "title") VALUES ('d750cfdd-ae7b-48a6-a2e0-d49e70e28686'::uuid, '') 3. INSERT INTO "app_child" ("parent_ptr_id", "body") VALUES ('d750cfdd-ae7b-48a6-a2e0-d49e70e28686'::uuid, '')
Python 3.6, PostgreSQL 9.6
Change History (10)
comment:1 by , 7 years ago
Summary: | Child model updates parent model with empty fields making an extra query in multi-inheritance use case → Child model updates parent model with empty fields making an extra query in multi-inheritance when parent model has custom PK |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 6 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Closing as a duplicate of #29260 because the issue has nothing to do with MTI and the other ticket has a larger discussion around how it should be solved.
comment:3 by , 5 years ago
Cc: | added |
---|---|
Resolution: | duplicate |
Status: | closed → new |
#31297 was opened and is a duplicate of this. This still occurs in Django 3.0. Reproduced at a6b3938afc0204093b5356ade2be30b461a698c5. It looks like this use-case wasn't picked up as part of #29260. Possibly related to #18305.
Simon, re-opening and CC-ing you to ask your opinion. (Thanks!)
comment:4 by , 5 years ago
Cc: | added |
---|
CC'ing myself since I reported #31297. Didn't know about this ticket while creating that one.
I'm up for fixing this issue if it is accepted.
comment:5 by , 5 years ago
Carlton, I agree that it's its own issue and that it's highly related to #18305.
I'd say the fact that save(force_insert=True)
doesn't work is a duplicate of #18305 and the fact save()
doesn't result in the same optimization as #29260 when a primary key default is defined in MTI is an omission of #29260.
comment:6 by , 5 years ago
Cheers Simon.
I'm up for fixing this issue if it is accepted.
Abhijeet, if you'd like to assign yourself and take a look that would be great. Reach out if you need any input.
Thanks.
comment:7 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Thank you. I'll come up with a patch soon.
comment:9 by , 5 years ago
Triage Stage: | Accepted → Ready for checkin |
---|---|
Version: | 2.0 → master |
That does look unexpected. Reproduced at cb7860ccedb199cb221c9e084b5104978b246356.