[Django] #29260: Django makes an extra UPDATE query when custom PK is evaluating before save.

30 views
Skip to first unread message

Django

unread,
Mar 25, 2018, 7:00:30 PM3/25/18
to django-...@googlegroups.com
#29260: Django makes an extra UPDATE query when custom PK is evaluating before
save.
-------------------------------------+-------------------------------------
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 |
-------------------------------------+-------------------------------------
Using model's instance:

{{{

class Account(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
title = models.TextField()

>> account = Account()
>> account.title = "abc"
>> account.save()

1. UPDATE "app_account" SET "title" = \'\', WHERE "app_account"."id" =
\'67c9327d-150e-419f-b493-0c2c59a045c3\'::uuid',
2. INSERT INTO "app_account" ("title", "id") VALUES (\'abc\', \'3d8c1b3c-
214a-4798-a0fa-d4c22c2b877f\'::uuid)

}}}

Using model's manager method:

{{{
>> Account.objects.create(title="abc")

1. INSERT INTO "app_account" ("title", "id") VALUES (\'abc\', \'3d8c1b3c-
214a-4798-a0fa-d4c22c2b877f\'::uuid)
}}}


Related issue? https://code.djangoproject.com/ticket/29129

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

Django

unread,
Mar 25, 2018, 7:17:41 PM3/25/18
to django-...@googlegroups.com
#29260: Django makes an extra UPDATE query when custom PK is evaluating before
save.
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by user0007:

Old description:

> Using model's instance:
>
> {{{
>
> class Account(models.Model):
> id = models.UUIDField(
> primary_key=True,
> default=uuid.uuid4,
> editable=False
> )
> title = models.TextField()
>
> >> account = Account()
> >> account.title = "abc"
> >> account.save()
>
> 1. UPDATE "app_account" SET "title" = \'\', WHERE "app_account"."id" =
> \'67c9327d-150e-419f-b493-0c2c59a045c3\'::uuid',
> 2. INSERT INTO "app_account" ("title", "id") VALUES (\'abc\', \'3d8c1b3c-
> 214a-4798-a0fa-d4c22c2b877f\'::uuid)
>
> }}}
>
> Using model's manager method:
>
> {{{
> >> Account.objects.create(title="abc")
>
> 1. INSERT INTO "app_account" ("title", "id") VALUES (\'abc\', \'3d8c1b3c-
> 214a-4798-a0fa-d4c22c2b877f\'::uuid)
> }}}
>

> Related issue? https://code.djangoproject.com/ticket/29129

New description:

Using a model's instance:

{{{

class Account(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
title = models.TextField()

>> account = Account()
>> account.title = "abc"
>> account.save()

1. UPDATE "app_account" SET "title" = \'\', WHERE "app_account"."id" =
\'67c9327d-150e-419f-b493-0c2c59a045c3\'::uuid',
2. INSERT INTO "app_account" ("title", "id") VALUES (\'abc\', \'3d8c1b3c-
214a-4798-a0fa-d4c22c2b877f\'::uuid)

}}}

Using a model's manager method:

{{{
>> Account.objects.create(title="abc")

1. INSERT INTO "app_account" ("title", "id") VALUES (\'abc\', \'3d8c1b3c-
214a-4798-a0fa-d4c22c2b877f\'::uuid)
}}}

Using a model's instance with `force_insert` argument:

{{{


>> account = Account()
>> account.title = "abc"

>> account.save(force_insert=true)

1. INSERT INTO "app_account" ("title", "id") VALUES (\'abc\', \'3d8c1b3c-
214a-4798-a0fa-d4c22c2b877f\'::uuid)
}}}


Related issue? https://code.djangoproject.com/ticket/29129

--

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

Django

unread,
Mar 26, 2018, 10:21:56 AM3/26/18
to django-...@googlegroups.com
#29260: Django makes an extra UPDATE query when custom PK is evaluating before
save.
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Tim Graham):

I'm not sure if the issue can or should be fixed. For the model you gave,
an instance will have an `id` from `default=uuid.uuid4`, so
[https://docs.djangoproject.com/en/stable/ref/models/instances/#how-
django-knows-to-update-vs-insert as documented] an `UPDATE` is tried
([https://github.com/django/django/blob/4554f9a783665d64b59c35b53ae71178e56ac783/django/db/models/base.py#L804-L805
code]). A fix might try to detect if the primary key came from a `default`
and if so, skip the update.

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

Django

unread,
Mar 26, 2018, 12:00:01 PM3/26/18
to django-...@googlegroups.com
#29260: Django makes an extra UPDATE query when custom PK is evaluating before
save.
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Simon Charette):

> A fix might try to detect if the primary key came from a default and if
so, skip the update.

I think we could use some kind of `self._state.adding and
self._meta.pk.default` heuristics to automatically set `force_insert=True`
on the last table/leaf child. That would break the following scenario
though.

{{{#!python
a = Account(pk='known-uuid-pk')
a.title = new_title
a.save() # expects an UPDATE here.
}}}

But I would argue that `force_update` should be passed in this case.

That wouldn't solve the MTI issue described in #29129 but that would do
for this case.

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

Django

unread,
Mar 26, 2018, 1:49:11 PM3/26/18
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primar key that
has a default

-------------------------------------+-------------------------------------
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


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

Django

unread,
Mar 27, 2018, 8:52:07 PM3/27/18
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default

-------------------------------------+-------------------------------------
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
-------------------------------------+-------------------------------------

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

Django

unread,
Apr 3, 2018, 10:42:27 PM4/3/18
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default
-------------------------------------+-------------------------------------
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 Windson yang):

@Tim Graham, should we still work on it?

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

Django

unread,
Apr 4, 2018, 11:49:42 AM4/4/18
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default
-------------------------------------+-------------------------------------
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 Tim Graham):

Simon's proposal seems fine to me.

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

Django

unread,
Dec 17, 2018, 8:21:58 AM12/17/18
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner:
| candypoplatte
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 candypoplatte):

* status: new => assigned
* owner: nobody => candypoplatte


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

Django

unread,
Dec 17, 2018, 12:59:06 PM12/17/18
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner:
| candypoplatte
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 Simon Charette):

Closed #29129 as a duplicate because the root of the issue is really the
primary key with a default and not MTI.

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

Django

unread,
Aug 17, 2019, 10:41:04 AM8/17/19
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Hasan
| Ramezani

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 Hasan Ramezani):

* owner: HyunTae Hwang => Hasan Ramezani
* has_patch: 0 => 1


Comment:

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

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

Django

unread,
Aug 19, 2019, 7:49:57 AM8/19/19
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Hasan
| Ramezani
Type: Bug | Status: closed

Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution: fixed
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 Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"85458e94e38c20e57939947ee515a1a53689659f" 85458e9]:
{{{
#!CommitTicketReference repository=""
revision="85458e94e38c20e57939947ee515a1a53689659f"
Fixed #29260 -- Skipped an UPDATE when adding a model instance with
primary key that has a default.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29260#comment:11>

Django

unread,
Aug 19, 2019, 7:50:31 AM8/19/19
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Hasan
| Ramezani
Type: Bug | Status: closed
Component: Database layer | Version: master

(models, ORM) |
Severity: Normal | Resolution: fixed
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 felixxm):

* version: 2.0 => master


--
Ticket URL: <https://code.djangoproject.com/ticket/29260#comment:12>

Django

unread,
Dec 30, 2019, 2:31:59 AM12/30/19
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Hasan
| Ramezani
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"9e14bc2135cb806b66374bac791c79344fff4ded" 9e14bc2]:
{{{
#!CommitTicketReference repository=""
revision="9e14bc2135cb806b66374bac791c79344fff4ded"
Refs #29260 -- Doc'd Model.save() behavior change in Django 3.0.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29260#comment:13>

Django

unread,
Dec 30, 2019, 2:32:02 AM12/30/19
to django-...@googlegroups.com
#29260: Remove UPDATE query when saving a new model instance with a primary key
that has a default
-------------------------------------+-------------------------------------
Reporter: user0007 | Owner: Hasan
| Ramezani
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

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

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"a04e6fb3557de79826d946c34bde6d049fd45c55" a04e6fb3]:
{{{
#!CommitTicketReference repository=""
revision="a04e6fb3557de79826d946c34bde6d049fd45c55"
[3.0.x] Refs #29260 -- Doc'd Model.save() behavior change in Django 3.0.

Backport of 9e14bc2135cb806b66374bac791c79344fff4ded from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29260#comment:14>

Reply all
Reply to author
Forward
0 new messages