Detect when onConflictDoUpdate does a insert vs a update

Hi, I was wondering if there is anyway to know when a onConflictDoUpdate does a update vs a insert. Thanks!
3 Replies
Sillvva
Sillvva4mo ago
I don't think there is a way. The output is the same either way. Something like this (without .returning())
{
"rows": [],
"fields": [],
"affectedRows": 1
}
{
"rows": [],
"fields": [],
"affectedRows": 1
}
You can try it here: https://drizzle.run/gixu7ads36xwsx06xedd7q1e Try running it with id: 1, which conflicts, and without. Alternative would be to run a select query to check for conflicting records, before running the insert or update.
aleclarson
aleclarson3mo ago
You can keep an updatedAt column in your table, and include it in your returning clause, then compare to timestamp cached before you ran the query.
Angelelz
Angelelz3mo ago
You can still use the returning trick even if you don't have an updatedAt column by using the excluded table to compare what's returned

Did you find this page helpful?