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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701892ebb7a09-a708d23a-e31f-4e28-82cb-0018f8149978-000000%40eu-central-1.amazonses.com.

Reply via email to