Opened 4 years ago
Closed 3 years ago
#32381 closed New feature (fixed)
Include number of rows matched in bulk_update() return value
Reported by: | Chris Jerdonek | Owned by: | Abhyudai |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | bulk_update |
Cc: | Tom Forbes, Diego Lima | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Currently, bulk_update()
returns None
, unlike update()
, which returns the number of rows matched.
It looks like it would be easy to add the same functionality to bulk_update()
since bulk_update()
simply calls update()
repeatedly:
https://github.com/django/django/blob/2b4b6c8af0aae8785bc1347cf1be2e8e70fd5ff3/django/db/models/query.py#L568
I.e. the return values could simply be added and returned.
Change History (24)
comment:1 by , 4 years ago
comment:2 by , 4 years ago
Cc: | added |
---|---|
Triage Stage: | Unreviewed → Accepted |
Also, if this feature is okayed, maybe the number could be returned in a way that would permit other useful numbers to be added to the return value in a backwards-compatible way later on. For example, the number could be returned as a property of a named tuple with one property. That way, if a new value is requested in the future, it could be added by adding a new property to the named tuple.
Do you have any specific statistics in mind?
comment:3 by , 4 years ago
Thanks.
Do you have any specific statistics in mind?
Not especially, but more because I don't know what might be available. The only things that occur to me right now are things like the number of queries made, and the batch size used for the function call. More usefully, though, if update()
were ever to grow the ability to report not just the number of rows matched but also the number of rows changed, that would definitely be of interest.
follow-up: 13 comment:4 by , 4 years ago
More usefully, though, if update() were ever to grow the ability to report not just the number of rows matched but also the number of rows changed, that would definitely be of interest.
Given that would likely require a signature change to update
if we ever want to support it I think that making bulk_update
return the sums of its update
s should be good enough for now.
comment:5 by , 4 years ago
I would second that: it’s good to be future proof, but we’re somewhat locked in to the current returned result due to update. Let’s just make it return the updated rows, which seems like a simple and sensible change.
comment:6 by , 4 years ago
Easy pickings: | set |
---|
comment:7 by , 4 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:8 by , 4 years ago
I took a look at this yesterday and due to the way bulk_update treats duplicates in objs
this might not be quite so straight forward as simply summing the returned values from repeated update()
calls. Duplicates split between batches will be counted twice.
comment:9 by , 4 years ago
There is now an (accepted) issue #32406 to make update()
return something different. Given that, might it make sense to reconsider making bulk_update()
future-proof now?
comment:10 by , 4 years ago
update
will keep returning int
unless returning
is specified otherwise it would break backward compatibility.
Can't this ticket be dedicated to having bulk_update
returning int
when returning
is not specified?
I don't see what needs to be made here to make bulk_update
future proof? How does making bulk_update -> int
prevent us from having bulk_update(returning) -> list
match the signature of update(returning) -> list
in the future?
comment:11 by , 4 years ago
Owner: | changed from | to
---|
comment:13 by , 4 years ago
Cc: | added |
---|---|
Owner: | changed from | to
Due to inactivity, I'm claiming this ticket. bhavikabadjate901 please come forward if you have done any work on this!
I'm taking the simplest approach first, which is bulk_update -> int
. As stated by Simon Charette:
I think that making
bulk_update
return the sums of itsupdate
s should be good enough for now.
and Tom Forbes:
Let’s just make it return the updated rows
Then, I'll stop and think a bit more about Chris Jerdonek's future-proofing and Tim McCurrach's duplicate edge case.
comment:14 by , 4 years ago
Has patch: | set |
---|
comment:15 by , 4 years ago
Needs documentation: | set |
---|
comment:16 by , 4 years ago
Needs documentation: | unset |
---|
comment:17 by , 4 years ago
PR ready for review at https://github.com/django/django/pull/14125. I did not handle duplicates, and did not future-proof bulk_update
.
I'm keeping an eye on #32406. If it gets patched, I'll update the returned value on bulk_update
. If we pin down an API that is generally agreed upon, I might claim that ticket as well.
comment:18 by , 4 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
comment:19 by , 3 years ago
Owner: | changed from | to
---|
comment:21 by , 3 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
comment:22 by , 3 years ago
Patch needs improvement: | set |
---|
comment:23 by , 3 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Also, if this feature is okayed, maybe the number could be returned in a way that would permit other useful numbers to be added to the return value in a backwards-compatible way later on. For example, the number could be returned as a property of a named tuple with one property. That way, if a new value is requested in the future, it could be added by adding a new property to the named tuple.