question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

afterCreate in Pool with better-sqlite3

See original GitHub issue

Environment

Knex version: 1.0.1

Bug

  1. After switching from sqlite3 to better-sqlite3 we are having issues with the following line of code:

knexConfig.pool = { afterCreate: (conn, cb) => conn.run("PRAGMA foreign_keys = ON", cb), };

  1. Error: TypeError: conn.run is not a function at afterCreate (src/shared/database/configurations/KnexConfigFactory.ts:9:128) at node:internal/util:360:7 at new Promise (<anonymous>) at afterCreate (node:internal/util:346:12) at create (node_modules/knex/lib/client.js:252:50)

Question

Is this a known bug or is there another way to use foreign keys with better-sqlite3?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
benjdlambertcommented, Jan 26, 2022

Yep - will try and take a look before the weekend!

3reactions
lviviercommented, Feb 5, 2022

better-sqlite3’sDatabase class doesn’t have a run() method, but it does have an exec() method. There’s also a pragma() method specifically for executing PRAGMA statements. From the docs:

It’s better to use this method instead of normal prepared statements when executing PRAGMA, because this method normalizes some odd behavior that may otherwise be experienced.

You can probably do something like (not tested):

(conn, cb) => {
  conn.pragma("foreign_keys = ON")
  cb()
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Installation | Knex.js
afterCreate callback (rawDriverConnection, done) is called when the pool aquires a new connection from the database server. done(err, connection) callback ...
Read more >
better-sqlite-pool - npm
A connection pool for the module better-sqlite3. Using this module to open pools and acquire connections, and release the connection once it has ......
Read more >
Database configuration - Strapi Developer Docs
pool. Optional, Database pooling options, Object, - ... afterCreate, Callback function to execute custom logic when the pool acquires a new connection.
Read more >
knex | Yarn - Package Manager
PostgreSQL: Explicit jsonb support for custom pg clients #5201; SQLite: Support returning with sqlite3 and better-sqlite3 #5285; MSSQL: Implement mapBinding ...
Read more >
Browse SQL Technical Problem Clusters - Code Grepper
... show query code after create table in sql · access mysql without sudo ... mysql disable root login · const pool =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found