#36683: Update with Distinct Produces Unintentional Results
-------------------------------------+-------------------------------------
Reporter: matthewshirley | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: orm,distinct,update | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Similar to #32433.
The ORM permits calling `update()` on a `Queryset` when a `distinct()` was
applied. This results in unforeseen consequences where a larger data set
will be updated than what is expected. Similar to the example provided in
#32433:
`Comment.objects.order_by('post_id',
'created_at').distinct('post_id').update(deleted=True)`
The developer may assume that this will delete only one comment per
`post_id`, however, the `distinct()` is ignored (as `UPDATE` has no
distinct) which results in the entire table being updated:
`UPDATE "post_comment" SET "deleted" = true'`
Since `delete()` already guards against this case, I believe `update()`
should behave consistently to protect the developer from mistakes, even
though applying a `distinct()` before an `update()` is generally unusual.
--
Ticket URL: <https://code.djangoproject.com/ticket/36683>
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 visit
https://groups.google.com/d/msgid/django-updates/0107019a1269fde8-62094a63-b9ae-45db-8f04-36cc5399af72-000000%40eu-central-1.amazonses.com.