Skip to content

Commit 65a3405

Browse files
committed
fix(docs): clarify binding behavior for undefined values in migration and data handling guides
1 parent 085c97a commit 65a3405

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

doc/migrating-from-better-sqlite3.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,21 @@ const db = enhance(new DatabaseSync("mydb.sqlite"));
320320
1. **Use `enhance()` for better-sqlite3 compatibility** - Wrap your database with `enhance()` to get `.transaction()` and `.pragma()` methods
321321
2. **Property name changes** - `.name``.location()`, `.open``.isOpen`, `.inTransaction``.isTransaction`
322322
3. **No virtual table API** - Use raw SQL if needed
323+
4. **`undefined` is not a valid binding value** - Both `node:sqlite` and `@photostructure/sqlite` reject `undefined` in parameter bindings (better-sqlite3 silently treated it as NULL). Use `null` instead. This commonly surfaces with ORMs like Knex that produce `undefined` in multi-row inserts for missing columns:
324+
325+
```javascript
326+
// better-sqlite3: worked (undefined treated as NULL)
327+
stmt.run("Alice", undefined);
328+
329+
// @photostructure/sqlite: throws "Provided value cannot be bound to SQLite parameter"
330+
stmt.run("Alice", undefined);
331+
332+
// Fix: use null explicitly
333+
stmt.run("Alice", null);
334+
335+
// When using Knex or other query builders, sanitize bindings:
336+
const bindings = bindings.map((v) => (v === undefined ? null : v));
337+
```
323338

324339
## Need help?
325340

doc/working-with-data.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ try {
361361

362362
## NULL Handling
363363

364+
Use `null` (not `undefined`) to bind SQL NULL values. Passing `undefined` as a binding throws an error, matching `node:sqlite` behavior:
365+
366+
```javascript
367+
stmt.run("Alice", null); // Correct: binds SQL NULL
368+
stmt.run("Alice", undefined); // Error: "Provided value cannot be bound to SQLite parameter"
369+
```
370+
364371
```javascript
365372
// Inserting NULL values
366373
const stmt = db.prepare("INSERT INTO users (name, email) VALUES (?, ?)");

0 commit comments

Comments
 (0)