[Django] #36683: Update with Distinct Produces Unintentional Results

9 views
Skip to first unread message

Django

unread,
Oct 23, 2025, 2:52:15 PM10/23/25
to django-...@googlegroups.com
#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.

Django

unread,
Oct 23, 2025, 3:11:03 PM10/23/25
to django-...@googlegroups.com
#36683: Update with Distinct Produces Unintentional Results
-------------------------------------+-------------------------------------
Reporter: Matt Shirley | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm,distinct,update | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matt Shirley):

* has_patch: 0 => 1

Comment:

https://github.com/django/django/pull/19997 (PR)
--
Ticket URL: <https://code.djangoproject.com/ticket/36683#comment:1>

Django

unread,
Oct 23, 2025, 3:14:17 PM10/23/25
to django-...@googlegroups.com
#36683: Update with Distinct Produces Unintentional Results
-------------------------------------+-------------------------------------
Reporter: Matt Shirley | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm,distinct,update | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36683#comment:2>

Django

unread,
Oct 23, 2025, 4:07:26 PM10/23/25
to django-...@googlegroups.com
#36683: Update with Distinct Produces Unintentional Results
-------------------------------------+-------------------------------------
Reporter: Matt Shirley | Owner: Matt
| Shirley
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm,distinct,update | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matt Shirley):

* owner: (none) => Matt Shirley
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36683#comment:3>

Django

unread,
Oct 24, 2025, 5:37:22 PM10/24/25
to django-...@googlegroups.com
#36683: Update with Distinct Produces Unintentional Results
-------------------------------------+-------------------------------------
Reporter: Matt Shirley | Owner: Matt
| Shirley
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm,distinct,update | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36683#comment:4>

Django

unread,
Oct 24, 2025, 6:02:21 PM10/24/25
to django-...@googlegroups.com
#36683: Update with Distinct Produces Unintentional Results
-------------------------------------+-------------------------------------
Reporter: Matt Shirley | Owner: Matt
| Shirley
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm,distinct,update | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matt Shirley):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/36683#comment:5>

Django

unread,
Oct 25, 2025, 11:44:06 AM10/25/25
to django-...@googlegroups.com
#36683: Update with Distinct Produces Unintentional Results
-------------------------------------+-------------------------------------
Reporter: Matt Shirley | Owner: Matt
| Shirley
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm,distinct,update | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36683#comment:6>

Django

unread,
Oct 25, 2025, 12:20:30 PM10/25/25
to django-...@googlegroups.com
#36683: Update with Distinct Produces Unintentional Results
-------------------------------------+-------------------------------------
Reporter: Matt Shirley | Owner: Matt
Type: | Shirley
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm,distinct,update | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* type: Bug => Cleanup/optimization
* version: 5.2 => dev

--
Ticket URL: <https://code.djangoproject.com/ticket/36683#comment:7>

Django

unread,
Oct 25, 2025, 12:21:38 PM10/25/25
to django-...@googlegroups.com
#36683: Update with Distinct Produces Unintentional Results
-------------------------------------+-------------------------------------
Reporter: Matt Shirley | Owner: Matt
Type: | Shirley
Cleanup/optimization | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: orm,distinct,update | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls <jacobtylerwalls@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"4744e9939b65d168c531e5e23d1ac8a4445ac7f9" 4744e99]:
{{{#!CommitTicketReference repository=""
revision="4744e9939b65d168c531e5e23d1ac8a4445ac7f9"
Fixed #36683 -- Added error message on QuerySet.update() following
distinct(*fields).
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36683#comment:8>
Reply all
Reply to author
Forward
0 new messages