For a table like this one:
CREATE TABLE Users(
id SERIAL PRIMARY KEY,
name TEXT UNIQUE
);
What would be the correct one-query insert for the following operation:
Given a user name
, insert a new record and return the new id
. But if the name
already exists, just return the id
.
I am aware of the new syntax within PostgreSQL 9.5 for ON CONFLICT(column) DO UPDATE/NOTHING
, but I can't figure out how, if at all, it can help, given that I need the id
to be returned.
It seems that RETURNING id
and ON CONFLICT
do not belong together.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…