#29260 closed Bug (fixed)
Remove UPDATE query when saving a new model instance with a primary key that has a default
Reported by: | user0007 | Owned by: | Hasan Ramezani |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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
Change History (14)
comment:1 by , 7 years ago
Description: | modified (diff) |
---|
comment:2 by , 7 years ago
comment:3 by , 7 years ago
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.
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.
comment:4 by , 7 years ago
Summary: | Django makes an extra UPDATE query when custom PK is evaluating before save. → Remove UPDATE query when saving a new model instance with a primar key that has a default |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:5 by , 7 years ago
Summary: | Remove UPDATE query when saving a new model instance with a primar key that has a default → Remove UPDATE query when saving a new model instance with a primary key that has a default |
---|
comment:8 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:9 by , 6 years ago
Closed #29129 as a duplicate because the root of the issue is really the primary key with a default and not MTI.
comment:10 by , 5 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
comment:12 by , 5 years ago
Version: | 2.0 → master |
---|
I'm not sure if the issue can or should be fixed. For the model you gave, an instance will have an
id
fromdefault=uuid.uuid4
, so as documented anUPDATE
is tried (code). A fix might try to detect if the primary key came from adefault
and if so, skip the update.