node-postgres-参数化查询-将uuid作为参数传递

时间:2019-03-30 20:44:28

标签: uuid node-postgres

我无法在node-postgres中使用参数化查询从文本值插入UUID列。 根据论坛回复尝试了许多建议,但没有用。 Pass UUID value as a parameter to the function

尝试了$ 1 :: text,只有$ 1,但给出了错误提示原因:错误:uuid的输入语法无效:

let id = uuid.v4();
if (result.id) {//if an input uuid is provided - else i could use a Postgres auto-increment
  id = result.id;
} 
//Tried JSON.parse(id), JSON.stringify(id) here
logger.debug('create id:', id);//uuid format for id 
let query = {
  text: 'INSERT INTO resource_dataset (id, type, metadata, storage_location) VALUES ($1::uuid, $2, $3, $4) RETURNING id, type, metadata, storage_location, created_on',
  values: ['${id}','${type}', '${saveResult.metadata}', '${storageLocation}']
};
//Also tried just id in the values - gives invalid json  
pool.query(query, (err, result1) => {
........
});

表定义:
    如果不存在,则创建表resource_dataset(       id UUID NOT NULL,       ....       主键(id)     );

TIA,

0 个答案:

没有答案