#28668: Add ON CONFLICT support to QuerySet.bulk_create() -------------------------------------+------------------------------------- Reporter: Tom Forbes | Owner: Tom | Forbes Type: New feature | Status: assigned Component: Database layer | Version: master (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 1 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by Дилян Палаузов): For a table like {{{CREATE TABLE t (id SERIAL PRIMARY KEY, name VARCHAR(10) UNIQUE, comment VARCHAR(10));}}} it is possible to tell which models were inserted with a query like {{{ WITH to_be_inserted AS (SELECT * FROM (VALUES ('name12', 'comment12'), ('name5', 'comment5'), ('name6', 'comment6')) as g(name, comment)), successfully_inserted AS ( INSERT INTO t ("name", "comment" ) SELECT * FROM to_be_inserted ON CONFLICT DO NOTHING RETURNING *) SELECT s.id FROM to_be_inserted AS b LEFT JOIN successfully_inserted AS s ON (b.name = s.name AND b.comment = s.comment); }}} where {{{to_be_inserted}}} contains the values that are going into the database. The returned column contains NULL for values that were presented in the database, and the id for the inserted rows. With the proposed changes {{{ - if connection.features.can_return_ids_from_bulk_insert: + if connection.features.can_return_ids_from_bulk_insert and not on_conflict == 'ignore': assert len(ids) == len(objs_without_pk) for obj_without_pk, pk in zip(objs_without_pk, ids): obj_without_pk.pk = pk }}} the for-loop will not work, as the amount of ids is not the same as the amount of obj_without_pk. -- Ticket URL: <https://code.djangoproject.com/ticket/28668#comment:9> 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/061.e50be14c0e4c2b49683b1c8840fc5ec8%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.