postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Dynamic on conflict definition

Open olmohake opened this issue 1 year ago • 1 comments

I am struggling to set up the SET part of my on conflict definition dynamically. Everything else working smoothly, but failing to pass upsert information. Would welcome any help.

const targetTable = "test"
const columns = ["id", "name", "geometry"]
const onConflict = { keys: ["id"], update: ["name", "geometry"]}

await sql`
  INSERT INTO ${sql(targetTable)} ${sql(data, columns)}
  ON CONFLICT (${sql(onConflict.keys.join(","))}) DO UPDATE
  SET ${sql(onConflict.update.map(column => `${column} = EXCLUDED.${column}`).join(",")};
`

olmohake avatar Jan 31 '25 19:01 olmohake

I'm also interested

Edweis avatar May 31 '25 09:05 Edweis