I was reading the documentation of SqlAlchemy regarding the upsert operation on postgres dialect at http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#insert-on-conflict-upsert
Is there a way to know if the upsert has been an insert or an update?
The documentation seems to omit this detail.
Just add a final RETURNING
clause with:
...
RETURNING (tbl.xmax = 0) AS inserted
Returns true
for inserted rows and false
for updated rows. This relies on an undocumented implementation detail, though. For a detailed explanation see:
Adding OIDs (like suggested in another answer) adds cost, bloats the table and burns OIDs (if your table isn't trivially small). That's why the general setting of default_with_oids
has been changed to off
a long time ago (Postgres 8.1). Quoting the manual:
The use of OIDs in user tables is considered deprecated, so most installations should leave this variable disabled. Applications that require OIDs for a particular table should specify
WITH OIDS
when creating the table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With