Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32388 closed Cleanup/optimization (fixed)

bulk_update() doesn't necessarily ignore duplicates.

Reported by: Tim McCurrach Owned by: Tim McCurrach
Component: Documentation Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

In the docs for bulk_update it says: "If objs contains duplicates, only the first one is updated." This is due to the way the query is constructed.

However this is only true for each SQL update. And if there are duplicates that fall into different batches, they will both (or all) be used as part of the update.

To Reproduce Error

>>> m1 = MyModel.objects.get(id=1)
>>> m2 = MyModel.objects.get(id=1)
>>> m1.name="a"
>>> m2.name="b"
>>> MyModel.objects.bulk_update([m1, m2], ['name'], batch_size=1)
>>> MyModel.objects.get(id=1).name
'b' 

Whilst the above is an extreme example, it demonstrates the point. If a large number of objects are being updated, you cannot currently rely on the behaviour that only the first instance of a duplicate will affect the update.

Change History (5)

comment:1 by Tim McCurrach, 4 years ago

Owner: changed from nobody to Tim McCurrach
Status: newassigned

comment:2 by Mariusz Felisiak, 4 years ago

Component: Database layer (models, ORM)Documentation
Easy pickings: set
Summary: bulk_update doesn't necessarily ignore duplicatesbulk_update() doesn't necessarily ignore duplicates.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

I don't think it is worth additional complexity (see #29968), we can clarify this in docs, e.g.

If ``objs`` contains duplicates in a batch, only the first one is updated.

comment:3 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 241da3f0:

Fixed #32388 -- Clarified QuerySet.bulk_update() caveat about duplicates for multiple batches.

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In e95a60b:

[3.2.x] Fixed #32388 -- Clarified QuerySet.bulk_update() caveat about duplicates for multiple batches.

Backport of 241da3f06ee0c6f436341cda5890b221ac453e3b from master

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In b920945d:

[3.1.x] Fixed #32388 -- Clarified QuerySet.bulk_update() caveat about duplicates for multiple batches.

Backport of 241da3f06ee0c6f436341cda5890b221ac453e3b from master

Note: See TracTickets for help on using tickets.
Back to Top