insert select from on duplicate key ignore postgres
Asked Answered
L

1

11

I've read from this source that you can do an insert on dupcliate key ignore in postgres, but I cannot seem to get this to work for a select from:

link

What I've seen you can do is:

insert into tab(id, val) values(1, 'nosert'), (2,
'nosert'), (3, 'nosert') on duplicate key ignore returning id;

Why can't I do something like this?

insert into table_one (field_one, field_two) select field_one, field_two from table_two on duplicate key ignore returning field_one;

I'm being told that I have syntax error near duplicate, but the query before then runs fine (although it colides on a duplicate index) and the rest of the query just adds the on duplicate key ignore returning field_one.

Is this not possible with select from?

Lipp answered 2/2, 2017 at 5:31 Comment(0)
L
10

Was able to resolve with on conflict do nothing for 9.5

insert into table_one (field_one, field_two) 
select field_one, field_two from table_two 
on conflict do nothing;
Lipp answered 2/2, 2017 at 5:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.