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 Tim Graham, 7 years ago

Summary: Child model updates parent model with empty fields making an extra query in multi-inheritance use caseChild model updates parent model with empty fields making an extra query in multi-inheritance when parent model has custom PK
Triage Stage: UnreviewedAccepted

That does look unexpected. Reproduced at cb7860ccedb199cb221c9e084b5104978b246356.

comment:2 by Simon Charette, 6 years ago

Resolution: duplicate
Status: newclosed

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 Carlton Gibson, 5 years ago

Cc: Simon Charette added
Resolution: duplicate
Status: closednew

#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 Abhijeet Viswa, 5 years ago

Cc: Abhijeet Viswa 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 Simon Charette, 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 Carlton Gibson, 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 Abhijeet Viswa, 5 years ago

Owner: changed from nobody to Abhijeet Viswa
Status: newassigned

Thank you. I'll come up with a patch soon.

comment:8 by Abhijeet Viswa, 5 years ago

Has patch: set

comment:9 by Mariusz Felisiak, 5 years ago

Triage Stage: AcceptedReady for checkin
Version: 2.0master

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In babd4126:

Fixed #29129 -- Skipped UPDATE when adding a model instance with inherited primary key that has a default.

Note: See TracTickets for help on using tickets.
Back to Top