Opened 3 months ago

Last modified 3 months ago

#29129 assigned Bug

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: Ashaba John
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no 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 (2)

comment:1 Changed 3 months ago by Tim Graham

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 Changed 3 months ago by Ashaba John

Owner: changed from nobody to Ashaba John
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top