Skip to content

Mark updated rows on conflict #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
IlyaSemenov opened this issue May 11, 2020 · 3 comments
Open

Mark updated rows on conflict #100

IlyaSemenov opened this issue May 11, 2020 · 3 comments

Comments

@IlyaSemenov
Copy link

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.

@IlyaSemenov IlyaSemenov changed the title Mark inserted rows on conflict Mark updated rows on conflict May 11, 2020
@IlyaSemenov
Copy link
Author

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

@talperetz
Copy link

@Photonios any plan for implementing this functionality?

@wesleykendall
Copy link

@IlyaSemenov @talperetz if you need a work-around, https://github.com/Opus10/django-pgbulk supports this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants