Re: [Django] #34698: Allow returning IDs in QuerySet.bulk_create() when updating conflicts. (was: Allow returning IDs in bulk_create with conflicts handling)

4 views
Skip to first unread message

Django

unread,
Jul 7, 2023, 1:03:10 AM7/7/23
to django-...@googlegroups.com
#34698: Allow returning IDs in QuerySet.bulk_create() when updating conflicts.
-------------------------------------+-------------------------------------
Reporter: Thomas C | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Chih Sean Hsu (added)
* stage: Unreviewed => Accepted


Comment:

Thanks for the ticket. I've checked and it works on PostgreSQL, MariaDB
10.5+, and SQLite 3.35+:
{{{#!diff
diff --git a/django/db/models/query.py b/django/db/models/query.py
index a5b0f464a9..f1e052cb36 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -1837,12 +1837,15 @@ class QuerySet(AltersData):
inserted_rows = []
bulk_return =
connection.features.can_return_rows_from_bulk_insert
for item in [objs[i : i + batch_size] for i in range(0,
len(objs), batch_size)]:
- if bulk_return and on_conflict is None:
+ if bulk_return and (on_conflict is None or on_conflict ==
OnConflict.UPDATE):
inserted_rows.extend(
self._insert(
item,
fields=fields,
using=self.db,
+ on_conflict=on_conflict,
+ update_fields=update_fields,
+ unique_fields=unique_fields,
returning_fields=self.model._meta.db_returning_fields,
)
)
}}}

Would you like to prepare a patch via GitHub PR? (docs changes and tests
are required)

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

Reply all
Reply to author
Forward
0 new messages