[Django] #32388: bulk_update doesn't necessarily ignore duplicates

3 views
Skip to first unread message

Django

unread,
Jan 26, 2021, 8:39:51 PM1/26/21
to django-...@googlegroups.com
#32388: bulk_update doesn't necessarily ignore duplicates
-------------------------------------+-------------------------------------
Reporter: Tim | Owner: nobody
McCurrach |
Type: Bug | Status: new
Component: Database | Version: 3.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In the
[https://docs.djangoproject.com/en/3.1/ref/models/querysets/#django.db.models.query.QuerySet.bulk_update
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32388>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 26, 2021, 8:40:05 PM1/26/21
to django-...@googlegroups.com
#32388: bulk_update doesn't necessarily ignore duplicates
-------------------------------------+-------------------------------------
Reporter: Tim McCurrach | Owner: Tim
| McCurrach
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim McCurrach):

* owner: nobody => Tim McCurrach
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/32388#comment:1>

Django

unread,
Jan 27, 2021, 2:13:17 AM1/27/21
to django-...@googlegroups.com
#32388: bulk_update() doesn't necessarily ignore duplicates.

-------------------------------------+-------------------------------------
Reporter: Tim McCurrach | Owner: Tim
Type: | McCurrach
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* type: Bug => Cleanup/optimization
* component: Database layer (models, ORM) => Documentation
* easy: 0 => 1
* stage: Unreviewed => Accepted


Comment:

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.
}}}

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

Django

unread,
Jan 28, 2021, 2:12:23 AM1/28/21
to django-...@googlegroups.com
#32388: bulk_update() doesn't necessarily ignore duplicates.
-------------------------------------+-------------------------------------
Reporter: Tim McCurrach | Owner: Tim
Type: | McCurrach
Cleanup/optimization | Status: closed
Component: Documentation | Version: 3.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"241da3f06ee0c6f436341cda5890b221ac453e3b" 241da3f0]:
{{{
#!CommitTicketReference repository=""
revision="241da3f06ee0c6f436341cda5890b221ac453e3b"
Fixed #32388 -- Clarified QuerySet.bulk_update() caveat about duplicates
for multiple batches.
}}}

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

Django

unread,
Jan 28, 2021, 2:12:31 AM1/28/21
to django-...@googlegroups.com
#32388: bulk_update() doesn't necessarily ignore duplicates.
-------------------------------------+-------------------------------------
Reporter: Tim McCurrach | Owner: Tim
Type: | McCurrach
Cleanup/optimization | Status: closed
Component: Documentation | Version: 3.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"e95a60bb64b2c78f025a44972e7d646fa8c85b2b" e95a60b]:
{{{
#!CommitTicketReference repository=""
revision="e95a60bb64b2c78f025a44972e7d646fa8c85b2b"
[3.2.x] Fixed #32388 -- Clarified QuerySet.bulk_update() caveat about
duplicates for multiple batches.

Backport of 241da3f06ee0c6f436341cda5890b221ac453e3b from master
}}}

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

Django

unread,
Jan 28, 2021, 2:12:53 AM1/28/21
to django-...@googlegroups.com
#32388: bulk_update() doesn't necessarily ignore duplicates.
-------------------------------------+-------------------------------------
Reporter: Tim McCurrach | Owner: Tim
Type: | McCurrach
Cleanup/optimization | Status: closed
Component: Documentation | Version: 3.1
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"b920945d0286673e5374dee60d329e5d2d1127cd" b920945d]:
{{{
#!CommitTicketReference repository=""
revision="b920945d0286673e5374dee60d329e5d2d1127cd"
[3.1.x] Fixed #32388 -- Clarified QuerySet.bulk_update() caveat about
duplicates for multiple batches.

Backport of 241da3f06ee0c6f436341cda5890b221ac453e3b from master
}}}

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

Reply all
Reply to author
Forward
0 new messages