在Nodejs和Postgres中input错误

我正在根据postgres提供的对象列表生成dynamic更新查询。 这是我的查询看起来像:

update loan_item_assignment as t set id = c.id, dateselectionid = c.dateselectionid, loanitemid = c.loanitemid, active = c.active, type = c.type from (values ( $1, $2, $3, $4, $5 ), ( $6, $7, $8, $9, $10 ), ( $11, $12, $13, $14, $15 ), ( $16, $17, $18, $19, $20 ), ( $21, $22, $23, $24, $25 ), ( $26, $27, $28, $29, $30 ), ( $31, $32, $33, $34, $35 ), ( $36, $37, $38, $39, $40 ) ) as c( id, dateselectionid, loanitemid, active, type ) where c.id = t.id returning * 

这里是我给它的值列表:

 [ 7, 35, 3, true, 'normal', 8, 35, 4, true, 'normal', 1, 35, 6, true, 'normal', 2, 35, 7, true, 'normal', 3, 35, 8, true, 'normal', 5, 35, 10, true, 'normal', 4, 35, 11, true, 'normal', 6, 35, 12, true, 'normal' ] 

据我所知,这些值正确匹配。 这是我看到的错误:

 { [error: operator does not exist: text = integer] name: 'error', length: 195, severity: 'ERROR', code: '42883', detail: undefined, hint: 'No operator matches the given name and argument type(s). You might need to add explicit type casts.', position: '448', internalPosition: undefined, internalQuery: undefined, where: undefined, schema: undefined, table: undefined, column: undefined, dataType: undefined, constraint: undefined, file: 'parse_oper.c', line: '726', routine: 'op_error' } 

这是最终运行查询的代码:

 var performQuery = function(text, values, cb) { pg.connect(connectionString, function(err, client, done) { client.query(text, values, function(err, result) { done(); if (!result) { console.log(err); cb([], err); } else { cb(result.rows, err); } }) }); } 

这里是表格定义:

 Table "public.loan_item_assignment" Column | Type | Modifiers | Storage | Stats target | Description -----------------+---------+-------------------------------------------------------------------+----------+--------------+------------- id | integer | not null default nextval('loan_item_assignment_id_seq'::regclass) | plain | | dateselectionid | integer | | plain | | loanitemid | integer | | plain | | active | boolean | | plain | | type | text | | extended | | Indexes: "loan_item_assignment_pkey" PRIMARY KEY, btree (id) Foreign-key constraints: "loan_item_assignment_dateselectionid_fkey" FOREIGN KEY (dateselectionid) REFERENCES date_selection(id) "loan_item_assignment_loanitemid_fkey" FOREIGN KEY (loanitemid) REFERENCES loan_item(id) 

Vitaly-t对我的回答的评论是解决scheme – 使用pg-promise库来生成查询,特别是用于生成多行更新查询的方法helpers.update ,如Node.js中的PostgreSQL多行更新所示。