我刚刚将node.js
应用程序上传到heroku,我的一个SQL查询失败了。查询是:
INSERT INTO countries (name, user_id, created_timestamp)
SELECT $1, $2, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM countries WHERE name = $1 FOR UPDATE)
失败
错误:子查询中不允许SELECT FOR UPDATE / SHARE
有谁知道为什么?如果我无法选择更新,我可以使用吗?
答案 0 :(得分:1)
这可能对您有用:
BEGIN;
LOCK TABLE countries IN SHARE MODE;
INSERT INTO countries (name, user_id, created_timestamp)
SELECT $1, $2, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT * FROM countries WHERE name = $1);
COMMIT;