We currently use mysql / knex, and I'm adding SQLite as a database for testing purposes. I'm getting
Knex:warning - sqlite does not support inserting default values. Set the
useNullAsDefault
flag to hide this warning. (see docs http://knexjs.org/#Builder-insert).
How does Knex handle default values? Does it just drop any defaults, or does it add in the defaults after an insert as following UPDATE
statements?
I don't want to change all of our codebase (swap out all default values), trying to do the minimal change that will allow me to run SQLite in our tests... concerned this will introduce bugs.
To set default value for a column in SQLite, use DEFAULT :CREATE TABLE customers ( id INTEGER PRIMARY KEY, store_code TEXT DEFAULT "store1" NOT NULL, name TEXT );
And note: The return knex( returns the Promise object to the caller, and the return dataArr returns the value to the caller's .
Generally, in SQLite default constraint will insert default value in a column in case if column value null or empty. We can add default constraint on the column while creating a new table using Create Statement or modifying / altering table using ALTER statement.
Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM.
I'm learning knex.js
so I can use it in a project involving PostgreSQL. While trying out Sqlite I came across this issue.
Turns out it's documented!
If one prefers that undefined keys are replaced with
NULL
instead ofDEFAULT
one may giveuseNullAsDefault
configuration parameter in knex config.
And they give this code:
var knex = require('knex')({
client: 'sqlite3',
connection: {
filename: "./mydb.sqlite"
},
useNullAsDefault: true
});
knex('coords').insert([{x: 20}, {y: 30}, {x: 10, y: 20}])
// insert into `coords` (`x`, `y`) values (20, NULL), (NULL, 30), (10, 20)"
This removed the warning message for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With