Skip to content

Instantly share code, notes, and snippets.

@jasheloper
Created November 26, 2019 00:58
Show Gist options
  • Save jasheloper/60dfd7b5f89d8e8b8e9e614482a61ba0 to your computer and use it in GitHub Desktop.
Save jasheloper/60dfd7b5f89d8e8b8e9e614482a61ba0 to your computer and use it in GitHub Desktop.
Foreign Key Setup - In Knex, foreign key restrictions don’t automatically work. Whenever using foreign keys in your schema, add the following code to your knexfile. This will prevent users from entering bad data into a foreign key column
development: {
client: 'sqlite3',
useNullAsDefault: true,
connection: {
filename: './data/database.db3',
},
// needed when using foreign keys
pool: {
afterCreate: (conn, done) => {
// runs after a connection is made to the sqlite engine
conn.run('PRAGMA foreign_keys = ON', done); // turn on FK enforcement
},
},
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment