---
title: Updating JSONB fields with Drizzle
description: 'hint: just use `sql` and COALESCE.'
slug: updating-jsonb-drizzle
type: snippet
date: July 11, 2024
---

I recently made a jsonb column in a postgres database and it took me a bit
to figure out how to update a single value without overwriting the entire
field. If you know a better way please let me know.

```tsx
import * as schema from './schema'

export async function updateThing(id: string, title: string) {
  return db
    .update(schema.thing)
    .set({
      metadata: sql`COALESCE(${schema.thing.metadata}, '{}')::jsonb || ${JSON.stringify({ title })}::jsonb`,
    })
    .where(eq(schema.thing.id, id))
}
```
