I'd like to get this working, but Postgres doesn't like having the WHERE
clause in this type of insert.
INSERT INTO keys(name, value) VALUES
('blah', 'true')
WHERE NOT EXISTS (
SELECT 1 FROM keys WHERE name='blah'
);
I'd like to get this working, but Postgres doesn't like having the WHERE
clause in this type of insert.
INSERT INTO keys(name, value) VALUES
('blah', 'true')
WHERE NOT EXISTS (
SELECT 1 FROM keys WHERE name='blah'
);
In Postgres, there is a really nice way to do that:
INSERT INTO keys(name, value)
SELECT 'blah', 'true'
WHERE NOT EXISTS (
SELECT 1 FROM keys WHERE name='blah'
);
hope that helps.-
WHERE
to make our selection of values invalid if we can't find the key. –
Argosy WHERE
clause belongs to the SELECT
part of the statement, rather than the overall INSERT
. –
Jussive In Postgresql 9.5 you can now use on conflict do nothing
if you also have a unique key or constraint:
insert into KEYS (name, value) values (
'blah', 'true') on conflict (name) do nothing;
insert into where not exists...
? More safe when used concurrently? –
Aerometry on conflict
clause only works on unique keys and constraints. for "normal columns", you should use the "where not exists". From the INSERT documentation on postgres: Specifies which conflicts ON CONFLICT takes the alternative action on by choosing arbiter indexes. Either performs unique index inference, or names a constraint explicitly
–
Sharpen © 2022 - 2024 — McMap. All rights reserved.