使用 postgresql 9.5 正确的 upsert 语法,下面的查询显示column reference "gallery_id" is ambiguous
错误,为什么?
var dbQuery = `INSERT INTO category_gallery (
category_id, gallery_id, create_date, create_by_user_id
) VALUES ($1, $2, $3, $4)
ON CONFLICT (category_id)
DO UPDATE SET
category_id = $1,
last_modified_date = $3,
last_modified_by_user_id = $4
WHERE gallery_id = $2`;
我尝试更改WHERE gallery_id = $2;
为 WHERE category_gallery.gallery_id = $2;
然后显示错误there is no unique or exclusion constraint matching the ON CONFLICT specification
,但我不想将 gallery_id 或 category_id 设置为唯一的,因为我想确保两列相同然后更新....
如何在 postgres 9.5 中正确执行 upsert?
如果ON CONFLICT
需要唯一列,我应该使用其他方法,如何?
我想确定多个列都冲突然后更新,正确的用法是什么
var dbQuery = `INSERT INTO category_gallery (
category_id, gallery_id, create_date, create_by_user_id
) VALUES ($1, $2, $3, $4)
ON CONFLICT (category_id, gallery_id)
DO UPDATE SET
category_id = $1,
last_modified_date = $3,
last_modified_by_user_id = $4
WHERE gallery_id = $2`;
var dbQuery = `INSERT INTO category_gallery (
category_id, gallery_id, create_date, create_by_user_id
) VALUES ($1, $2, $3, $4)
ON CONFLICT (category_id AND gallery_id)
DO UPDATE SET
category_id = $1,
last_modified_date = $3,
last_modified_by_user_id = $4
WHERE gallery_id = $2`;
表(category_id,gallery_id 不是唯一列)
category_id | gallery_id | create_date | create_by_user_id | last_modified_date | last_modified_by_user_id
1 | 1 | ...
1 | 2 | ...
2 | 2 | ...
1 | 3 | ...