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`;
我try 将WHERE gallery_id = $2;
改为WHERE category_gallery.gallery_id = $2;
,然后显示错误there is no unique or exclusion constraint matching the ON CONFLICT specification
,但I don't want to set gallery_id or category_id as unique因为我想确保两列相同,然后进行更新....
如何正确地在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`;
表(类别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 | ...