onConflictDoUpdate() ID entry

How to get ID of entry in onConflictDoUpdate, where conflict appeared?
4 Replies
Luxaritas
Luxaritas17mo ago
I’m not sure I understand your question - you shouldn’t need to provide that ID in order to do the update, and you have to explicitly specify the column you’re checking for conflicts on yourself via the target parameter Maybe it would be helpful to have an example of what you’re trying to do?
Matoslav
MatoslavOP17mo ago
I have a local array of objects that also contain an ID. When there is a conflict, I want to get the ID of the item so that I can take the value from this field and update it this way I can update multiple items in case of multiple conflicts.
Luxaritas
Luxaritas17mo ago
This is Postgres, right? I think what you want is to use the virtual EXCLUDED table so that you can reference the other fields of the item that failed to insert to construct the update I don’t think Drizzle implements that directly right now, but you should be able to do something like
set: {name: sql`excluded.name`}
set: {name: sql`excluded.name`}
Or it might be excluded."name"? Unfortunately I’m not well versed in Postgres
rphlmr ⚡
rphlmr ⚡17mo ago
you can do that: https://discord.com/channels/1043890932593987624/1043890932593987627/1143921817002709062
await db.insert(table.products)
.values(products)
.onConflictDoUpdate({
target: table.products.id,
set: { productName: sql`EXCLUDED.productName` },
});
await db.insert(table.products)
.values(products)
.onConflictDoUpdate({
target: table.products.id,
set: { productName: sql`EXCLUDED.productName` },
});
On conflict on products.id, it will update productName.
Want results from more Discord servers?
Add your server