$returningId() can not read properties of undefined id

Hey everyone! 👋
I’m running into an issue with onDuplicateKeyUpdate and $returningId(). I keep getting an error:
“Cannot read properties of undefined (reading ‘id)

I use drizzle with mysql (Planetscale)

Here’s the relevant snippet:
await db
  .insert(schema.category)
  .values(data)
  .onDuplicateKeyUpdate({ set: { id: sql`id` } })
  .$returningId();



What I expect this to do:
  1. Insert a new row into ⁠schema.category with whatever is in ⁠data.

  2. If there’s a duplicate key, update the existing row’s ⁠id column to the same value .
  3. Return the ⁠id of the inserted or updated row. Instead, I’m hitting the runtime error.
What could be going wrong?

Thanks in advance for any tips or pointers! 🙏
Was this page helpful?