【发布时间】:2016-04-22 16:35:26
【问题描述】:
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 | ...
【问题讨论】:
标签: sql postgresql upsert postgresql-9.5