我收到一个奇怪的错误:
{ __cid: '__cid9',
method: 'insert',
options: undefined,
bindings:
[ 500,
'Dinner',
'10/02/2015 7:57 PM',
'09/29/2015 8:00 PM',
'Grand Plaza',
1 ],
sql: 'insert into "expense" ("amount", "description", "due_date", "payment_date", "vendor_id") values ($1, $2, $3, $4, select "vendor_id" from "vendor" where "name" = $5 limit $6)',
returning: undefined }
error: syntax error at or near "select"
at [object Object].Connection.parseE (/.../node_modules/pg/lib/connection.js:534:11)
at [object Object].Connection.parseMessage (/.../node_modules/pg/lib/connection.js:361:17)
at Socket.<anonymous> (/.../node_modules/pg/lib/connection.js:105:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
我已经使用那些值剪切和粘贴运行原始SQL,它可以正常工作。
这是产生错误的代码:
Promise.each subbudget.expenses, (expense) ->
vendor.get(expense.vendor).then (vendor_id) ->
knex('expense').insert(
due_date: expense.dueDate
vendor_id: (knex.first("vendor_id").from("vendor").where({name: vendor_id}))
amount: expense.amount
description: expense.description
payment_date: expense.paidDate
)
编辑(部分解决方案): 问题似乎是SELECT语句周围缺少括号。 Knex提供.wrap(),它只适用于raw和.as(),它只适用于嵌套语句;由于某种原因,这不符合嵌套语句的条件,因此我不能围绕它获得括号。有什么想法吗?
答案 0 :(得分:0)
knex.raw(“(”+“knex.first(”vendor_id“)。from(”vendor“)。where({name:vendor_id})。toString()+”)“)
不是最干净的,但是使用.toString(),然后将它包装在.raw()
中