django-postgres-extra
django-postgres-extra copied to clipboard
Mark updated rows on conflict
Currently with on_conflict(...).insert(...)
(as well as insert_and_get
and bulk_insert
) there is no way to know which rows were inserted and which have been updated in-place (unlike Django's update_or_create
which returns a tuple of obj, created
).
However, Postgres provides such trick with RETURNING *, (xmax = 0)
(see https://stackoverflow.com/a/47001830/189806).
It would be great if that was supported natively by the library.
FWIW, I ended up using this (definitely ugly) monkey patch:
import psqlextra.compiler
class PostgresInsertCompiler(psqlextra.compiler.PostgresInsertCompiler):
def _rewrite_insert_update(self, sql, params, returning):
return super()._rewrite_insert_update(
sql, params, returning + ",(xmax = 0) AS _is_new"
)
psqlextra.compiler.PostgresInsertCompiler = PostgresInsertCompiler
@Photonios any plan for implementing this functionality?
@IlyaSemenov @talperetz if you need a work-around, https://github.com/Opus10/django-pgbulk supports this