2

Is it possible to retrieve the information for whether an upsert inserted a new value or updated an old one? Specifically I want to try to retrieve the id of the new inserted row if an insert occurred, or None if no insert occurred.

If it's helpful, I'm using python's psycog2 to interact with Postgres.

My Upsert looks somewhat like this:

INSERT INTO user (user_id, name, date, phone) VALUES (%(user_id)s,%(name)s, %(date)s, %(phone)s) ON CONFLICT (user_id, name) DO NOTHING

EDIT: I've seen returning, but it's unclear whether this would work as intended with upsert. example here:

 INSERT INTO persons (lastname,firstname) VALUES ('Smith', 'John') RETURNING id;

Any help would be appreciated!

chris
  • 1,869
  • 4
  • 29
  • 52
  • theortically you can do the opposite. add a column and assign its value to id if `ON CONFLICT` happened. and return its value. But this will be the opposite - returning id when `update` happended and null if `insert` – Vao Tsun Jun 13 '16 at 06:51

0 Answers0