I have a table that looks like this:
CREATE TABLE IF NOT EXISTS list (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tok TEXT,
sid TEXT NOT NULL,
aid TEXT,
hash TEXT,
qtt SMALLINT,
p DECIMAL,
UNIQUE (tok, sid, aid),
UNIQUE (sid, qtt, hash)
);
I want to create a dynamic insert function with node-pg, the query of which looks roughly like this:
INSERT (tok, sid, aid, qtt, hash, p)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (tok, sid, aid, qtt, hash)
DO UPDATE SET p = $6;
and when I try to insert values of (NULL, string, NULL, int, string, decimal)
(values that match one of the composite keys) it throws this error:
error: there's no unique or exclusion constraint matching the ON CONFLICT specification.
This has to do less with node-pg but how I made the INSERT
query itself it seems. My intent is for a single upsert query to handle list items of both "UNIQUE
types" in case of a conflict, no other questions about composite uniques give a firm answer on how to do it with composite keys with overlapping columns.
Assuming there will only be rows that match either one of the unique keys, what do I do with the insert or constraints to make it work properly? Is there a way to also keep it a dynamic one-query solution?