我在下面有這段代碼
INSERT INTO table1 (code, value)
VALUES ('code1', 'value1')
ON CONFLICT (code, value)
WHERE code = 'code1'
DO
UPDATE SET value = 'value1';
當我嘗試運行此代碼時,我得到
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
所以我嘗試在“更新”部分添加一個“where”子句,但隨后出現錯誤
ERROR: column reference "code" is ambiguous
如何正確創建帶有“更新”的“插入”?
uj5u.com熱心網友回復:
條件中的錯誤WHERE被拋出,因為不清楚您的意思是table1.code(舊版本)還是EXCLUDED.code(新版本)。你必須在那里明確。
但是這個WHERE條件不會阻止最初的問題,即沒有唯一約束或索引(code, value)。如果你想使用,你必須創建這樣一個約束或索引INSERT ... ON CONFLICT。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/526449.html
標籤:PostgreSQL上插
下一篇:函式current_user
