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.

PRAGMA foreign keys = ON;

See original GitHub issue

How can I activate foreign keys check before performing an Insert ? I’ve tried

knex.raw('PRAGMA foreign keys = ON;'); 

But it’s not working.

Any thoughts ?

Thanks !

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:4
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

36reactions
tgriessercommented, Sep 2, 2014

So, the code there doesn’t actually do anything, as the db calls are async and it’s never executed with .exec or .then (or related promise methods). What you want to do is add an afterCreate on the pool and run one of the api’s for the sqlite3 as needed:

# Initialize knex library
knex = require('knex')(
  client : 'sqlite3'
  connection : 
    filename: "./db/scriptor.db"
  pool:
    afterCreate: (conn, cb) ->
       conn.run('PRAGMA foreign_keys = ON', cb);
)

That being said, I realized recently that the foreign_keys pragma being off by default in sqlite3 is a bit inconsistent. @vschoettke or @bendrucker, what do you think about changing this to be run by default (configurable to turn off in the config options) so foreign keys are consistent with the other databases.

6reactions
JcBernackcommented, Aug 30, 2015

+1 totally got me by surprise that SQLite has it off by default

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLite Foreign Key Support
Foreign key ON DELETE and ON UPDATE clauses are used to configure actions that take place when deleting rows from the parent table...
Read more >
SQLite Foreign Key: Enforce Relationships Between Tables
If the SQLite library is compiled with foreign key constraint support, the application can use the PRAGMA foreign_keys command to enable or disable...
Read more >
SQLite: pragma foreign_keys
pragma foreign_keys = on enforces foreign keys. This is usually necessary because by default, SQLite does not enforce foreign keys.
Read more >
SQLite3 "forgets" to use foreign keys - Stack Overflow
I know that foreign keys are deactivated by default and have to be activated with PRAGMA foreign_keys = ON; . In my Ruby...
Read more >
foreign_key_list - Using SQLite [Book] - O'Reilly
The foreign_key_list pragma lists all the foreign key references that are part of the specified table. That list will contain one row for...
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