Fail to cast boolean column: SQLite3 can only bind numbers, strings, bigints, buffers, and null
See original GitHub issueI got that error because one table has a ~date~ boolean column, the following is the code that I’m using
import type { ColumnType, Generated, Insertable, Selectable } from 'kysely';
export interface TaskTable {
id: Generated<string>;
title: string;
completed: boolean;
createdAt: ColumnType<Date, string | undefined, never>;
}
export type Task = Selectable<TaskTable>;
export type CreateTask = Pick<Insertable<TaskTable>, 'title'>
import Database from 'better-sqlite3';
import { Kysely, SqliteDialect } from 'kysely';
export interface DB {
tasks: TaskTable;
}
export const db = new Kysely<DB>({
dialect: new SqliteDialect({
database: new Database('kysely.db'),
}),
});
import { randomUUID } from 'node:crypto';
export class TaskService {
constructor(private readonly db: Kysely<DB>) {}
async create(newTask: CreateTask): Promise<Task> {
const task = await this.db /* The error was thrown here */
.insertInto('tasks')
.values({
id: randomUUID(),
title: newTask.title,
completed: false,
})
.returningAll()
.executeTakeFirstOrThrow();
return task;
}
async findAll(): Promise<Task[]> {
return this.db.selectFrom('tasks').selectAll().execute();
}
}
-- The DDL
CREATE TABLE tasks (
id VARCHAR (36) PRIMARY KEY NOT NULL,
title TEXT CHECK (ifnull(length(title), 0) > 0) NOT NULL,
completed BOOLEAN DEFAULT (false) NOT NULL,
createdAt DATETIME DEFAULT (datetime('now', 'localtime')) NOT NULL
);
Stacktrace
TypeError: SQLite3 can only bind numbers, strings, bigints, buffers, and null at SqliteConnection.executeQuery (/home/user/experimenting-with-kysely/node_modules/.pnpm/kysely@0.19.12/node_modules/kysely/dist/cjs/dialect/sqlite/sqlite-driver.js:56:28) at /home/user/experimenting-with-kysely/node_modules/.pnpm/kysely@0.19.12/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:36:45 at DefaultConnectionProvider.provideConnection (/home/user/experimenting-with-kysely/node_modules/.pnpm/kysely@0.19.12/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:26) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async DefaultQueryExecutor.executeQuery (/home/user/experimenting-with-kysely/node_modules/.pnpm/kysely@0.19.12/node_modules/kysely/dist/cjs/query-executor/query-executor-base.js:35:16) at async InsertQueryBuilder.execute (/home/user/experimenting-with-kysely/node_modules/.pnpm/kysely@0.19.12/node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:418:24) at async InsertQueryBuilder.executeTakeFirst (/home/user/experimenting-with-kysely/node_modules/.pnpm/kysely@0.19.12/node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:431:26) at async InsertQueryBuilder.executeTakeFirstOrThrow (/home/user/experimenting-with-kysely/node_modules/.pnpm/kysely@0.19.12/node_modules/kysely/dist/cjs/query-builder/insert-query-builder.js:443:24) at async TaskService.create (/home/user/experimenting-with-kysely/src/services/TaskService.ts:12:18) at async TaskController.create (/home/user/experimenting-with-kysely/src/controllers/TaskController.ts:10:18)
Is this error caused by a limitation of the driver?
- Yes
how can I cast the boolean to number and back to boolean using Kysely?
Dependency versions
better-sqlite3 v7.6.2 kysely v0.19.12 typescript v4.7.4
OS
Manjaro 21.3.4 x86_64 Node.js 16.15.1
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Error when passing javascript true/false as sqlite parameters
Fail to cast boolean column: SQLite3 can only bind numbers, strings, bigints, buffers, and null koskimas/kysely#123.
Read more >sqlite3 can only bind numbers, strings, bigints, buffers, and null ...
Describe the bug When I want to change the value of a boolean field in the sqlite3 database, I encounter the following error:...
Read more >Bind Boolean value in Sqlite - Stack Overflow
SQLite does not distinguish booleans from ints. So you can just use bind_int. The only real storage classes are NULL, INTEGER, REAL, TEXT, ......
Read more >Bug when converting string to boolean? - SQLite Forum
It is neither a NULL. I expected it should implicitly convert the string to a number/Boolean when I compare it to a number/Boolean....
Read more >SQLiteODBC Documentation - ch-werner.de
This installs the SQLite ODBC driver and creates a System DSN. ... default empty OEMCP (boolean) Win32 only: if true, translate strings from/to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@igalklebanov I use branded types SqliteBoolean and SqliteDateTime with cast helper.
This already does the conversion in the other direction:
The other direction is the tricky one.