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

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Django
#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.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/051.07fcee7bfe2f17c4acf7014266ac8928%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.5339e9e45ca53473b748849122e25396%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.fb8ccdaf9d20dc9a4e5bf0cbdc5b6ce2%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.b636e671f6e23a74a2cae56a6ff38f86%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29260: Remove UPDATE query when saving a new model instance with a primar key that has a default (was: Django makes an extra UPDATE query when custom PK is evaluating before save.)

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.65609974c9a0e93a64a9516e8eaa9e90%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29260: Remove UPDATE query when saving a new model instance with a primary key that has a default (was: Remove UPDATE query when saving a new model instance with a primar key that has a default)

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.fc49af91a4e8ed706880dd76fd613314%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29260: Remove UPDATE query when saving a new model instance with a primary key that has a default

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.4de9518dc56533f3f203297f4abb17d3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29260: Remove UPDATE query when saving a new model instance with a primary key that has a default

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.8b9f30aed40155d1c24fb879078016ff%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29260: Remove UPDATE query when saving a new model instance with a primary key that has a default

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.f1c191c8cc9964419acf26d898d67c61%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29260: Remove UPDATE query when saving a new model instance with a primary key that has a default

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.86a4062bc0609934fc8467dbda555dde%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29260: Remove UPDATE query when saving a new model instance with a primary key that has a default

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.6d65076e25cab4892a9e721c22dda954%40djangoproject.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29260: Remove UPDATE query when saving a new model instance with a primary key that has a default

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.8a9c7993ea9ec86b21401a460e8f0af0%40djangoproject.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29260: Remove UPDATE query when saving a new model instance with a primary key that has a default

Django
In reply to this post by Django
#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.d2d1f8898a0476461f7f27019f4d8565%40djangoproject.com.