#29590: get_or_create and update_or_create should not have defaults as an
optional
parameter
-------------------------------------+-------------------------------------
Reporter: Álex Córcoles | Owner: nobody
Type: New feature | 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 Álex Córcoles):
Hi, yeah, sorry, typed that out in a hurry.
Yesterday we had a bug where someone wrote:
{{{
models.Foo.objects.update_or_create(
foo=foo,
bar=bar,
spam=spam,
)
}}}
, when the correct code was:
{{{
models.Foo.objects.update_or_create(
foo=foo,
bar=bar,
defaults={
spam=spam,
}
)
}}}
This passed code review and even had 100% test coverage, but as it just
had a test case for the `update` case and not for the `or_create` case, we
didn't prevent the bug.
My argument here is that most calls to `update_or_create` or
`get_or_create` need a non-empty `defaults` parameter, so making it a
mandatory parameter can prevent bugs, at the cost of the actual calls not
needing `defaults` just need to type `defaults={},`. While people still
can write that when they shouldn't, it catches the eye in a code review.
Yes, this breaks compat, but it probably will also uncover hidden bugs,
and maybe it has a net positive effect.
Kind regards,
Álex
--
Ticket URL: <https://code.djangoproject.com/ticket/29590#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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.d41e8bee40927763560e230f95455b18%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.