Kysely

Update

The update operation allows you to modify existing records in a table. You can update single or multiple columns, use expressions, and filter the rows to update.

Basic Update

db.updateTable('users')
  .set({
    first_name: 'Jennifer',
    last_name: 'Aniston',
  })
  .where('id', '=', 1)
  .execute()

Update with Expressions

db.updateTable('users')
  .set((eb) => ({
    age: eb('age', '+', 1),
    last_login: new Date(),
    full_name: sql`concat(first_name, ' ', last_name)`,
  }))
  .execute()

Update with Returning

// PostgreSQL only
const updated = await db
  .updateTable('users')
  .set({ status: 'inactive' })
  .where('last_login', '<', someDate)
  .returning(['id', 'email'])
  .execute()

Update with Join

db.updateTable('users')
  .set((eb) => ({
    status: eb.ref('orders.status'),
  }))
  .innerJoin('orders', 'orders.user_id', 'users.id')
  .where('orders.id', '=', orderId)
  .execute()

Update with Subquery

db.updateTable('users')
  .set({
    rank: (eb) =>
      eb
        .selectFrom('rankings')
        .select('rank')
        .where('rankings.user_id', '=', eb.ref('users.id'))
        .limit(1),
  })
  .execute()

On this page