Mrugesh Vaghela
Mrugesh Vaghela

Reputation: 305

KNEX Undefined binding(s) detected when compiling SELECT query

var knex = require('knex')(config);
var bookshelf = require('bookshelf')(knex);
var SKU = bookshelf.Model.extend({
    tableName: 'skus',

});
SKU.where('id', undefined).fetch().then(function (skus) {
    if (skus) console.log(skus.toJSON());
}).catch(function (err) {
    console.error(err);
});

It throws

Undefined binding(s) detected when compiling SELECT query.

It is working fine with 0.11.5, it stopped working with 0.11.6 onwards. I am pointing to 0.13.0 right now.

useNullAsDefault: true

works fine with insert queries but not select queries. Is there any flag i should pass to resolve this error?

Upvotes: 14

Views: 39554

Answers (3)

Kazimir Podolski
Kazimir Podolski

Reputation: 348

If you are putting JSON into parameters, check for undefined - knex has a bug for this and gives misleading error message.

Upvotes: 0

Aayush Bhattacharya
Aayush Bhattacharya

Reputation: 1934

parameter mismatch in payload

the parameter which sql query is expecting and the parameter you sending in requestbody is not matching. in my case sql was expecting parameter "gradeId" but in requestBody i was sending "postId"

Upvotes: 4

Mikael Lepistö
Mikael Lepistö

Reputation: 19718

.where('id', undefined) does not mean anything in SQL. You cannot be querying something that is not there.

Maybe you wanted to query where id IS NULL? With knex it can be done like this: .whereNull('id')

With earlier knex versions it would have been just ignored, but starting from 0.12.x.

So to have equivalent functionality with newer (and actually it is compatible with older < 0.12 versions too) knex versions you should do:

SKU.fetch().then(function (skus) {
    if (skus) console.log(skus.toJSON());
}).catch(function (err) {
    console.error(err);
});

Unless bookshelf is adding some extra magic there...

The useNullAsDefault: true option Is used only for when inserting multiple rows in one insert. It does not make sense to use it unless you are using sqlite (check last example of http://knexjs.org/#Builder-insert).

Upvotes: 19

Related Questions