NodeJS 是否可以使用pgp.helpers.update进行枚举和时间戳?

vvppvyoh  于 2023-02-21  发布在  Node.js
关注(0)|答案(1)|浏览(95)

预期行为

pgp.helpers.update应该能够更新枚举和时间戳。
我有一个表("myTable"),它具有以下列

id (varchar)
comparator (type enum named as comparator_type with values ['eq', 'ne', 'leq', 'geq', 'gt', 'lt'])
updatedAt (timestamp)

要更新的条目包括

entries = [
    {
        id: "someId",
         comparator: "gt",
        updatedAt: new Date().toISOString()
    }
]
// Using pgp.helpers.update
const updateQuery = pgp.helpers.update(entries, ["?id", "comparator", "updatedAt"], "myTable") + '  WHERE v.id = t.id';
console.log(updateQuery);

// printing it here for reference
/**
* update "myTable" as t set "comparator"=v."comparator","updatedAt"=v."updatedAt" from (values('someId','gt','0.92','2023-02-17T19:46:38.723Z')) as v("id","comparator","updatedAt") WHERE v.id = t.id
**/

实际行为

它没有更新type enumtimestamp。这是以下错误

重现步骤

以下代码用于运行生成的查询

await pgDb.any(updateQuery);

我得到了以下错误

{
    "name": "error",
    "severity": "ERROR",
    "code": "42804",
    "hint": "You will need to rewrite or cast the expression.",
    "position": "112",
    "file": "parse_target.c",
    "line": "587",
    "routine": "transformAssignedExpr",
    "query": "<query above>"
}

当我尝试在Dbever中运行相同的查询时,它开始给我以下信息,

ERROR: column "comparator" is of type comparator_type but expression is of type text. Hint: You will need to rewrite or cast the expression.

如果我将"comparator"=v."comparator"更改为"comparator"='gt',则会出现下一个错误

ERROR: column "updatedAt" is of type timestamp without time zone but expression is of type text. Hint: You will need to rewrite or cast the expression.

所以很明显,pg-promise生成的查询不起作用。
环境

  • pg-promise版本:11.2.0
  • 操作系统类型(Linux/Windows/Mac):Linux操作系统
  • Node. js的版本:版本16.19.0
我创建的gihub问题的链接-https://github.com/vitaly-t/pg-promise/issues/866
osh3o9ms

osh3o9ms1#

我使用pg.helpers.ColumnSet()来解决我的查询。它支持强制转换,工作起来很有魅力。
PS:我想知道Column和ColumnSet之间的区别是什么。

entries = [
    {
        id: "someId",
         comparator: "gt",
        updatedAt: new Date().toISOString()
    }
]
const cs = new pg.helpers.ColumnSet([
    "?id",
    {
       name: "comparator",
       cast: "comparator_type"
    }, 
    {
       name: "updatedAt",
       cast: "timestap"
    }
]);

// Using pgp.helpers.update
const updateQuery = pgp.helpers.update(entries, cs, "myTable") + '  WHERE v.id = t.id';

相关问题