I have a demo table
CREATE TABLE items (
id SERIAL primary key,
user_id integer,
name character varying,
created timestamp with time zone default now()
);
And I want a single query to run and first insert data, then return primary key using returning id and then update the same table with the returned id.
INSERT INTO items (name) values ('pen') RETURNING id as idd
update items set user_id=(select idd) where id=(select idd)
but the above command doesn't work and throws syntax error. Any help will be appriciated.
itemstable that sets columnuser_idto the generatedidvalue, instead of performing an UPDATE immediately after an INSERT? In any case, why do you store the ID twice in the same table? Doesn't that contradict normalization?