本文介绍了KNEX编译SELECT查询时检测到未定义的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

抛出

Undefined binding(s) detected when compiling SELECT query.

它工作正常,0.11.5,从0.11.6起停止工作。我现在指向0.13.0。

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?

推荐答案

.where('id', undefined)在SQL中没有任何意义。你不能查询那些不存在的东西。

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

也许你想查询其中id为IS NULL ?使用knex可以这样做: .whereNull('id')

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

使用早期的knex版本,它会已被忽略,但从0.12.x开始。

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

因此,为了获得与更新版本相同的功能(实际上它与旧的< 0.12版本兼容)你应该做的knex版本:

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...

useNullAsDefault:true 选项仅用于在一个插入中插入多行时。除非你使用sqlite,否则使用它是没有意义的(查看)。

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

这篇关于KNEX编译SELECT查询时检测到未定义的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:54