Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does knex handle default values in SQLite?

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.

like image 628
ripper234 Avatar asked Dec 08 '16 02:12

ripper234


People also ask

How do I change the default value in SQLite?

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 );

What does KNEX query return?

And note: The return knex( returns the Promise object to the caller, and the return dataArr returns the value to the caller's .

How do I add a default value to a column in SQLite?

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.

Is KNEX an ORM?

Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM.


1 Answers

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 of DEFAULT one may give useNullAsDefault 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.

like image 66
ArchNoob Avatar answered Oct 16 '22 03:10

ArchNoob