#34698: Allow returning IDs in bulk_create with conflicts handling
-------------------------------------+-------------------------------------
               Reporter:  Thomas C   |          Owner:  nobody
                   Type:  New        |         Status:  new
  feature                            |
              Component:  Database   |        Version:  4.2
  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          |
-------------------------------------+-------------------------------------
 Currently, when using `bulk_create` with a conflict handling flag turned
 on (e.g. `ignore_conflicts` or `update_conflicts`), the primary keys are
 not set in the returned queryset, as documented in `bulk_create`.

 While I understand using `ignore_conflicts` can lead to PostgreSQL not
 returning the IDs when a row is ignored (see
 [https://stackoverflow.com/questions/34708509/how-to-use-returning-with-
 on-conflict-in-postgresql this SO thread]), I don't understand why we
 don't return the IDs in the case of `update_conflicts`.

 For instance:

 {{{
 MyModel.objects.bulk_create([MyModel(...)], update_conflicts=True,
 update_fields=[...], unique_fields=[...])
 }}}

 generates a query without a `RETURNING my_model.id` part:

 {{{
 INSERT INTO "my_model" (...)
 VALUES (...)
     ON CONFLICT(...) DO UPDATE ...
 }}}

 If I append the `RETURNING my_model.id` clause, the query is indeed valid
 and the ID is returned (checked with PostgreSQL).

 I investigated a bit and
 
[https://github.com/django/django/blob/7414704e88d73dafbcfbb85f9bc54cb6111439d3/django/db/models/query.py#L1840-L1858
 this in Django source] is where the `returning_fields` gets removed.

 I believe we could discriminate the cases differently so as to keep those
 `returning_fields` in the case of `update_conflicts`.

 This would be highly helpful when using `bulk_create` as a bulk upsert
 feature.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34698>
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/010701892c0d096a-8c740b5c-11f6-487d-94ce-75e89b6d3f30-000000%40eu-central-1.amazonses.com.

Reply via email to