如何使用nodejs pg-promise库将具有uuid数组的logging插入到pg表中

我需要在我的数据库中有一个表,其中包含一个单一的列是一个uuid对象(uuid []types的数组)

但是当我尝试使用名为pg-promise的nodejs库插入到它时,它失败

我得到以下错误消息告诉我,我需要重写演员或expression式

{"name":"error","length":206,"severity":"ERROR","code":"42804","hint":"You will need to rewrite or cast the expression.","position":"230","file":"src\\backend\\parse r\\parse_target.c","line":"510","routine":"transformAssignedExpr"} 

这是奇怪的,因为我有绝对没有问题,当我尝试进入一个单一的uuid到另一列在同一个确切的表(我没有代表uuid的问题,顺便说一句,我创build它们作为一个文本variables从另一个库,但他们是普通的旧文本variables)

我也没有问题,当我尝试input一个TEXT对象数组到同一列(万一我更改表具有一个TEXT []列而不是UUID []列)

这是我的代码

 //////////////// var Promise = require('bluebird'); var pgpLib = require('pg-promise'); var pgp = pgpLib(); var cn = confUtil.pgDbConnectionConfiguration(); var db = pgp(cn); ////////////////// var newEntity={}; newEntity.hash = uuid.v4(); newEntity.location = {X:2394876,Y:2342342}; newEntity.mother = uuid.v4(); newEntity.timestamp = Date.now(); newEntity.content = {content:"blah"}; newEntity.sobList = [uuid.v4(),uuid.v4(),uuid.v4()]; addEntity (newEntity); //////////////////// function addEntity(newEntity) { var insertEntityQueryPrefix='insert into entities ('; var insertEntityQueryMiddle=') values ('; var insertEntityQueryPostfix=""; var insertEntityQuery=""; Object.keys(newEntity).forEach(function(key){ insertEntityQueryPrefix=insertEntityQueryPrefix+'"'+key+'",'; insertEntityQueryPostfix=insertEntityQueryPostfix+'${'+key+'},'; }); insertEntityQueryPrefix=insertEntityQueryPrefix.slice(0,-1); insertEntityQueryPostfix=insertEntityQueryPostfix.slice(0,-1)+")"; insertEntityQuery=insertEntityQueryPrefix+insertEntityQueryMiddle+insertEntityQueryPostfix; //longStoryShort this is how the query template i used looked like /* "insert into entities ("hash","location","mother","timestamp","content","sobList") values (${hash},${location},${mother},${timestamp},${content},${sobList})" */ //and this is the parameters object i fed to the query i ran it when it failed /* { "hash": "912f6d85-8b47-4d44-98a2-0bbef3727bbd", "location": { "X": 2394876, "Y": 2342342 }, "mother": "87312241-3781-4d7c-bf0b-2159fb6f7f74", "timestamp": 1440760511354, "content": { "content": "bla" }, "sobList": [ "6f2417e1-b2a0-4e21-8f1d-31e64dea6358", "417ade4b-d438-4565-abd3-a546713be194", "e4681d92-0c67-4bdf-973f-2c6a900a5fe4" ] } */ return db.tx(function () { var processedInsertEntityQuery = this.any(insertEntityQuery,newEntity); return Promise.all([processedInsertEntityQuery]) }) .then( function (data) { return newEntity; }, function (reason) { throw new Error(reason); }); } 

插入UUID-s数组是一种特殊情况,需要显式types转换,因为您将UUID-s作为文本string数组传递给uuid[]types。

您需要更改INSERT查询:将${sobList}replace${sobList} ${sobList}::uuid[] 。 这将指示PostgeSQL将string数组转换为UUID-s数组。

与你的问题无关,当执行一个请求时,你不需要在Promise.all里面使用Promise.all 。 您可以简单地从插入请求中返回结果:

 return this.none(insertEntityQuery,newEntity); 

尽pipe使用事务来执行单个请求同样是毫无意义的:)

UPDATE

pg-promise的最新版本支持自定义types格式 ,因此您可以编写自定义的查询格式types,避免显式types转换。

对于在数组中使用UUID-s的示例,您可以实现自己的UUIDtypes:

 function UUID(value) { this.uuid = value; this._rawDBType = true; // force raw format on output; this.formatDBType = function () { return this.uuid.v4(); }; }