Drizzle | SQL Toggle value
PostgreSQL
MySQL
SQLite
This guide assumes familiarity with:
- Get started with PostgreSQL, MySQL and SQLite
- Update statement
- Filters and not operator
- Boolean data type in MySQL and SQLite
To toggle a column value you can use update().set()
method like below:
import { eq, not } from 'drizzle-orm';
const db = drizzle(...);
await db
.update(table)
.set({
isActive: not(table.isActive),
})
.where(eq(table.id, 1));
update "table" set "is_active" = not "is_active" where "id" = 1;
Please note that there is no boolean type in MySQL and SQLite. MySQL uses tinyint(1). SQLite uses integers 0 (false) and 1 (true).