#31321: Unexpected behavior of `update_or_create`, misleading flag `created`.
-------------------------------------+-------------------------------------
Reporter: plushchaynikolay | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: get_or_create | Triage Stage:
update_or_create created | Unreviewed
primary_key |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):
* status: new => closed
* resolution: => wontfix
Comment:
`get_or_create()` doesn't override existing objects so it's not affected
by described behavior.
About `update_or_create()`, it's
[https://docs.djangoproject.com/en/3.0/ref/models/querysets/#update-or-
create documented] that this is as a shortcut to boilerplatish a concrete
code, it's also [https://docs.djangoproject.com/en/3.0/topics/db/queries
/#copying-model-instances documented] how Django behaves when you update
`pk` with `None`. If you will take these two into account then this
behavior is expected. I'm not sure why you created a new model instance
and used `model_to_dict()` instead of passing dictionary directly to
`defaults`.
IMO documenting this edge case or changing `created` to `found` would be
even more misleading. Changing behavior for `AutoField`'s would be
backward incompatible and I don't see a reason to do this.
--
Ticket URL: <https://code.djangoproject.com/ticket/31321#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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/074.c0a343434d9ca749d25f26662f02dd76%40djangoproject.com.