在Nodejs和Postgres中输入错误

时间:2016-09-25 16:59:36

标签: node.js postgresql node.js-pg

我正在根据postgres提供的对象列表生成动态更新查询。这就是我的查询:

'INSERT INTO news (id, title, pic, content, author, time) VALUES(NULL, :title, :picture, :content, :author, :time)

这是我给它的价值表:

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);
       }
     })
   });

}

1 个答案:

答案 0 :(得分:1)

Vitaly-t对我的回答的评论是解决方案 - 使用pg-promise库生成查询,特别是用于生成多行更新查询的方法helpers.update,如{{3}所示}}