postgresql - Knex.js SQL syntax error near 'select' -
i'm getting odd error:
{ __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 "expense" ("amount", "description", "due_date", "payment_date", "vendor_id") values ($1, $2, $3, $4, select "vendor_id" "vendor" "name" = $5 limit $6)', returning: undefined } error: syntax error @ or near "select" @ [object object].connection.parsee (/.../node_modules/pg/lib/connection.js:534:11) @ [object object].connection.parsemessage (/.../node_modules/pg/lib/connection.js:361:17) @ socket.<anonymous> (/.../node_modules/pg/lib/connection.js:105:22) @ socket.emit (events.js:107:17) @ readableaddchunk (_stream_readable.js:163:16) @ socket.readable.push (_stream_readable.js:126:10) @ tcp.onread (net.js:538:20)
i have run raw sql value cut , paste , works fine.
this code thats generating error:
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 )
edit (partial solution): issue seems parentheses missing around select statement. knex offers .wrap(), works on raw, , .as(), works on nested statements; reason not qualify nested statement, can't parentheses around it. ideas?
knex.raw("(" + knex.first("vendor_id").from("vendor").where({name: vendor_id}).tostring() + ")")
not cleanest, use .tostring(), wrap in .raw()
Comments
Post a Comment