[Django] #29129: Child model updates parent model with empty fields making an extra query in multi-inheritance use case

19 views
Skip to first unread message

Django

unread,
Feb 12, 2018, 4:38:51 PM2/12/18
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance use case
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 2.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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

--
Ticket URL: <https://code.djangoproject.com/ticket/29129>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 13, 2018, 9:59:14 AM2/13/18
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK

-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


Comment:

That does look unexpected. Reproduced at
cb7860ccedb199cb221c9e084b5104978b246356.

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:1>

Django

unread,
Feb 13, 2018, 5:43:16 PM2/13/18
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Ashaba
| John
Type: Bug | Status: assigned

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ashaba John):

* owner: nobody => Ashaba John
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:2>

Django

unread,
Jun 29, 2018, 2:25:40 PM6/29/18
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Ashaba
| John
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by user0007):

Replying to [comment:2 Ashaba John]:
Any chance to see fix for that in 2.1 release?

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:3>

Django

unread,
Jun 29, 2018, 3:18:46 PM6/29/18
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Ashaba
| John
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Ashaba John):

Replying to [comment:3 user0007]:


> Replying to [comment:2 Ashaba John]:
> Any chance to see fix for that in 2.1 release?

Hey,

Sorry for the delayed implementation. Will work on it and we should have
it in the 2.1 release. Thanks

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:4>

Django

unread,
Jul 10, 2018, 8:14:25 AM7/10/18
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: (none)
Type: Bug | Status: new

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ashaba John):

* owner: Ashaba John => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:5>

Django

unread,
Dec 17, 2018, 12:57:40 PM12/17/18
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* status: new => closed
* resolution: => duplicate


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:2>

Django

unread,
Feb 25, 2020, 5:55:17 AM2/25/20
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* cc: Simon Charette (added)
* status: closed => new
* resolution: duplicate =>


Comment:

#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!)

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:3>

Django

unread,
Feb 25, 2020, 6:12:03 AM2/25/20
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhijeet Viswa):

* cc: Abhijeet Viswa (added)


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:4>

Django

unread,
Feb 25, 2020, 8:38:46 AM2/25/20
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:5>

Django

unread,
Feb 25, 2020, 8:52:54 AM2/25/20
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:6>

Django

unread,
Feb 25, 2020, 8:54:53 AM2/25/20
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Abhijeet
| Viswa
Type: Bug | Status: assigned

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhijeet Viswa):

* owner: nobody => Abhijeet Viswa


* status: new => assigned


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:7>

Django

unread,
Feb 26, 2020, 10:43:01 PM2/26/20
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Abhijeet
| Viswa
Type: Bug | Status: assigned
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhijeet Viswa):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/12496 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:8>

Django

unread,
Mar 5, 2020, 6:10:01 AM3/5/20
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Abhijeet
| Viswa
Type: Bug | Status: assigned
Component: Database layer | Version: master

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* version: 2.0 => master
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:9>

Django

unread,
Mar 5, 2020, 6:29:56 AM3/5/20
to django-...@googlegroups.com
#29129: Child model updates parent model with empty fields making an extra query in
multi-inheritance when parent model has custom PK
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Abhijeet
| Viswa
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"babd4126853e48594b61e8db71a83d7bdd929b9c" babd4126]:
{{{
#!CommitTicketReference repository=""
revision="babd4126853e48594b61e8db71a83d7bdd929b9c"
Fixed #29129 -- Skipped UPDATE when adding a model instance with inherited
primary key that has a default.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29129#comment:10>

Reply all
Reply to author
Forward
0 new messages