我有个奇怪的错误:

{ __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()只在嵌套语句上工作;由于某种原因,这不符合嵌套语句的条件,因此我不能在它周围加括号。有什么想法吗?

最佳答案

knex.raw(“(”+knex.first(“vendor_id”).from(“vendor”).where({name:vendor_id}.toString()+”)
不是最干净的,但是使用.toString(),然后将其包装在.raw()中

关于postgresql - ``选择''附近的Knex.js SQL语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32915891/

10-14 01:41