kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.47k stars 267 forks source link

Update to set boolean value to opposite value #951

Closed randomGitBeing132 closed 5 months ago

randomGitBeing132 commented 5 months ago

Hi, i have a boolean value isEnabled and I would like to set it to the opposite value in my update statement

UPDATE table
SET is_enabled = ~is_enabled
WHERE table_id = 1

How do i translate this to kysely syntax?

Thank you

igalklebanov commented 5 months ago

Hey 👋

Use the NOT keyword.

await db
  .updateTable('table')
  .set('is_enabled', (eb) => eb.not('is_enabled'))
  .where('table_id', '=', 1)
  .execute();
randomGitBeing132 commented 5 months ago

Thank you for the help, but i got this error upon using it

Error: Incorrect syntax near the keyword \'not\'.\n at RequestTokenHandler.onErrorMessage

igalklebanov commented 5 months ago

What dialect are you using?

randomGitBeing132 commented 5 months ago

What dialect are you using?

MSSQL

igalklebanov commented 5 months ago

Use expression builder's neg method:

import { SqlBool, sql } from "kysely";

await db
  .updateTable("table")
  .set("is_enabled", (eb) => eb.neg("is_enabled"))
  .where("table_id", "=", 1)
  .execute()
igalklebanov commented 5 months ago

@randomGitBeing132 see updated answer.