错误:从 Bookshelf.js save() 编译 SELECT 时检测到未定义的绑定
Posted
技术标签:
【中文标题】错误:从 Bookshelf.js save() 编译 SELECT 时检测到未定义的绑定【英文标题】:Error: Undefined binding(s) detected when compiling SELECT from Bookshelf.js save() 【发布时间】:2020-05-30 17:37:05 【问题描述】:在使用 oracledb 客户端(以下版本)测试 Bookshelf 模型插入时,我遇到了一个奇怪的错误。我用 mysql、pg 和 sqlite3 客户端测试了相同的代码,在这个操作中没有问题。
我的模型是这样的:
Promotion = bookshelf.model('Promotion',
tableName: 'promotions'
)
触发错误的操作是这样的:
Promotion.forge(image:"image","featured":false,"price":"2.33","name":"name","description":"description").save(null, method:'insert' )
插入似乎已在数据库中执行并提交,但客户端返回此错误:
(node:1098008) UnhandledPromiseRejectionWarning: Error: Undefined binding(s) detected when compiling SELECT. Undefined
column(s): [promotions.id] query: select * from (select "promotions".* from "promotions" where "promotions"."image" = ?
and "promotions"."featured" = ? and "promotions"."price" = ? and "promotions"."name" = ? and "promotions"."description
" = ? and "promotions"."id" = ?) where rownum <= ?
at Oracledb_Compiler.toSQL (/home/user/src/tests/app/api/node_modules/knex/lib/query/compiler.js:101:13)
at Builder.toSQL (/home/user/src/tests/app/api/node_modules/knex/lib/query/builder.js:77:44)
at /home/user/src/tests/app/api/node_modules/knex/lib/runner.js:30:36
at /home/user/src/tests/app/api/node_modules/knex/lib/runner.js:253:24
at tryCatcher (/home/user/src/tests/app/api/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/user/src/tests/app/api/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/home/user/src/tests/app/api/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (/home/user/src/tests/app/api/node_modules/bluebird/js/release/promise.js:649:10)
at Promise._settlePromises (/home/user/src/tests/app/api/node_modules/bluebird/js/release/promise.js:729:18)
at _drainQueueStep (/home/user/src/tests/app/api/node_modules/bluebird/js/release/async.js:93:12)
at _drainQueue (/home/user/src/tests/app/api/node_modules/bluebird/js/release/async.js:86:9)
at Async._drainQueues (/home/user/src/tests/app/api/node_modules/bluebird/js/release/async.js:102:5)
at Immediate.Async.drainQueues (/home/user/src/tests/app/api/node_modules/bluebird/js/release/async.js:15:14)
at processImmediate (internal/timers.js:439:21)
at process.topLevelDomainCallback (domain.js:130:23)
(node:1098008) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing
inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
使用这个版本:
书架 1.1.0(同样测试了 1.0.1 错误) knex 0.20.9(也是 0.20.8) oracledb 4.2.0非常感谢您提前提供的帮助。
【问题讨论】:
当绑定值之一是undefined
时会发生这种情况
保存后自动刷新模型的功能可能存在问题,尽管我不明白为什么它只会在使用 OracleDB 时表现不同。如果你不带任何参数直接调用.save()
会发生什么?
是的,我认为这个问题是因为在成功保存后返回已保存对象的功能,因为执行了保存(插入)但获取(选择)返回对象是触发错误。如果我在没有任何参数的情况下使用 save(),则调用会成功,但它会在数据库中执行 UPDATE,而不是插入。
我不确定我们是否正在重现相同的错误,所以我继续创建了一个新问题:***.com/questions/61312488/…
【参考方案1】:
我有同样的问题。将新实例保存到数据库后,选择语句失败。像这样修复它:
await new BusinessOwnerSpecialty(newSpecialty)
.save(null,autoRefresh:false)
autoRefresh:false 选项修复了该问题。 来源:https://bookshelfjs.org/api.html#Model-instance-save
【讨论】:
以上是关于错误:从 Bookshelf.js save() 编译 SELECT 时检测到未定义的绑定的主要内容,如果未能解决你的问题,请参考以下文章