Opened 13 hours ago

Last modified 11 hours ago

#36683 assigned Bug

Update with Distinct Produces Unintentional Results

Reported by: Matt Shirley Owned by: Matt Shirley
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords: orm, distinct, update
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (3)

comment:2 by Simon Charette, 12 hours ago

Triage Stage: UnreviewedAccepted

comment:3 by Matt Shirley, 11 hours ago

Owner: set to Matt Shirley
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top