Search code examples
javascriptsqlknex.jsobjection.js

Why is all my data defaulting to NULL, while using Knex.js/Objection? I'm not passing useNullAsDefault at all


I'm not passing useNullAsDefault, and I've even tried using useNullAsDefault: false.

const knex = Knex({
    client: 'pg',
    connection: 'postgres://postgres:@localhost:5432/db_development',
});

Solution

  • useNullAsDefault affects only the case where you are inserting multiple rows in one insert and some of the rows are missing some keys.

    You need to setup your schema with default values to prevent NULLs being inserted as default values. You don't have to add that configuration parameter... it is provided just to maintain backwards compatibility for old projects which has been created many years ago.