can onConflictDoUpdate coalesce? #1749
-
hi, Suppose my table schema is (id: number (primary key), name: string, age: number). I have an existing record (1, "john", 20). Now I want to upsert (1, "john", null) where age=null:
I want to get the final record (1, "john", 20) instead of (1, "john", null). This needs drizzle to coalesce the provided null age with the existing 20. I can workaround by reading the existing record and manually create a coalesced row to upsert, but it is not efficient and bad DX. Can Drizzle do this? Thanks! |
Beta Was this translation helpful? Give feedback.
Answered by
Angelelz
Jan 4, 2024
Replies: 1 comment 1 reply
-
Can you check if this would work? let row = { id: 1, name: "john", age: null }
await drizzle(db).insert(users).values(row)
.onConflictDoUpdate(
{
target: users.id,
set: {
...row,
age: sql`coalesce(${row.age}, excluded.age)`
}
}).returning(); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
jiangok2006
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you check if this would work?