【发布时间】:2013-11-13 20:35:31
【问题描述】:
给定以下架构(作为示例)
CREATE TABLE "test" (
"id" int,
"title" varchar
);
在 NodeJS 中,我尝试使用以下内容进行更新
client.query(
'WITH new_vals ("id","title") AS (VALUES($1,$2)) ' +
'UPDATE "test" "t" SET "id"=nv.id, "title"=nv.title ' +
'FROM new_vals nv ' +
'WHERE "t"."id"=nv.id;',
[1,'test'],
function(err, res){ ... }
);
它给了我以下错误:'error: operator does not exist: integer = text'
好的,让我们尝试停止使用"t"."id"=nv.id,只重用一个可用参数:
client.query(
'WITH new_vals ("id","title") AS (VALUES($1,$2)) ' +
'UPDATE "test" "t" SET "id"=nv.id, "title"=nv.title ' +
'FROM new_vals nv ' +
'WHERE "t"."id"=$1;',
[1,'test'],
function(err, res){ ... }
);
仍然'错误:运算符不存在:整数=文本'。
好的,现在让我们添加另一个$3 占位符:
client.query(
'WITH new_vals ("id","title") AS (VALUES($1,$2)) ' +
'UPDATE "test" "t" SET "id"=nv.id, "title"=nv.title ' +
'FROM new_vals nv ' +
'WHERE "t"."id"=$3;',
[1,'test',1],
function(err, res){ ... }
);
另一个错误:'error: column "id" is of type integer but expression is of type text'
我完全迷路了。为什么不在 WHERE 中使用 int 运算符类?这里有什么问题?
奇怪的是,没有WITH 子句的标准UPDATE 形式可以正常工作...
如果您需要使用代码:https://gist.github.com/kolypto/7455957
【问题讨论】:
标签: node.js postgresql sql-update operators postgresql-9.1