upsert with excluded using column name

I'm trying to do a an upsert like so
await db.insert(user).values(values).onConflictDoUpdate({
target: user.id,
set: {
// Defined in schema as full_name
fullName: sql`EXCLUDED.${user.fullName.name}`
}
})
await db.insert(user).values(values).onConflictDoUpdate({
target: user.id,
set: {
// Defined in schema as full_name
fullName: sql`EXCLUDED.${user.fullName.name}`
}
})
When I try to do this, I'm getting error PostgresError: syntax error at or near "$265". If I do
fullName: sql`EXCLUDED.full_name`
fullName: sql`EXCLUDED.full_name`
, it works. But I would like to do so using the .name property as it will be easier to catch if there's an issue. How do I do it?
Solution:
Hello @vr7bd. You can follow this guide to do upsert https://orm.drizzle.team/learn/guides/upsert In your case should be ...
Drizzle ORM - Upsert Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Jump to solution
2 Replies
Solution
Mykhailo
Mykhailo7mo ago
Hello @vr7bd. You can follow this guide to do upsert https://orm.drizzle.team/learn/guides/upsert In your case should be
await db.insert(user).values(values).onConflictDoUpdate({
target: user.id,
set: {
// Defined in schema as full_name
fullName: sql.raw(`excluded.${user.fullName.name}`)
}
})
await db.insert(user).values(values).onConflictDoUpdate({
target: user.id,
set: {
// Defined in schema as full_name
fullName: sql.raw(`excluded.${user.fullName.name}`)
}
})
Drizzle ORM - Upsert Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
vr7bd
vr7bdOP7mo ago
Yup working thanks!
Want results from more Discord servers?
Add your server