crudely-typed icon indicating copy to clipboard operation
crudely-typed copied to clipboard

Add upsert queries

Open danvk opened this issue 2 years ago • 0 comments

Not 100% clear to me whether these can be generated -- I believe pg requires the name of the constraint that's violated.

Or maybe not. There's an example of this sort of query being generated here: https://github.com/jawj/mostly-ormless

INSERT INTO "appleTransactions"
("accountId", "environment", "latestReceiptData", "originalTransactionId") 
VALUES ($1, $2, $3, $4), ($5, $6, $7, $8) 
ON CONFLICT ("environment", "originalTransactionId") DO UPDATE 
SET ("accountId", "latestReceiptData") = 
  ROW(EXCLUDED."accountId", EXCLUDED."latestReceiptData") 
RETURNING *, 
  CASE xmax WHEN 0 THEN 'INSERT' ELSE 'UPDATE' END AS "$action"

The ROW(EXCLUDED) syntax is unfamiliar to me but looks useful.

danvk avatar Apr 05 '22 14:04 danvk