postgresql Postgres:-冲突时没有唯一约束进行更新

gpnt7bae  于 2023-02-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(242)

我有一个带有唯一约束(item,date,sales_category)的revaluation_of_table表。我必须更新或插入stock_revaluation表中的数据。所以我尝试了这些代码。

select item,date,sales_category from arpan.stock_revaluation
group by item,date,sales_category
having count(item)>1

这个显示stock_revaluation表没有重复。所以我写了这段代码来获得结果。

INSERT INTO arpan.revaluation_of_table_test (
    item
    ,date
    ,purchase
    ,price
    ,other_adjustments
    ,stock
    ,sold
    ,sold_from_stock
    ,sold_from_purchase
    ,total_remaining
    ,remaining_stock
    ,remaining_purchase
    ,realised
    ,reserved
    ,"Total_value_stock"
    ,month
    ,sales_category
    )
SELECT item
    ,date
    ,purchase
    ,price
    ,other_adjustments
    ,stock
    ,sold
    ,sold_from_stock
    ,sold_from_purchase
    ,total_remaining
    ,remaining_stock
    ,remaining_purchase
    ,realised
    ,reserved
    ,"Total_value_stock"
    ,month
    ,sales_category
FROM arpan.stock_revaluation ON CONFLICT (item, date, sales_category) DO UPDATE
SET item = excluded.item
    ,date = excluded.date
    ,purchase = excluded.purchase
    ,price = excluded.price
    ,other_adjustments = excluded.other_adjustments
    ,stock = excluded.stock
    ,sold = excluded.sold
    ,sold_from_stock = excluded.sold_from_stock
    ,sold_from_purchase = excluded.sold_from_purchase
    ,total_remaining = excluded.total_remaining
    ,remaining_stock = excluded.remaining_stock
    ,remaining_purchase = excluded.remaining_purchase
    ,realised = excluded.realised
    ,reserved = excluded.reserved
    ,"Total_value_stock" = excluded."Total_value_stock"
    ,month = excluded.month
    ,sales_category = excluded.sales_category

但是我收到这个错误。错误:没有与ON CONFLICT规范匹配的唯一约束或排除约束。请帮助我解决此问题
我看到以下内容:-No unique or exclusion constraint matching the ON CONFLICT,但未找到解决方案

vfh0ocws

vfh0ocws1#

这是我的错误,我在revaluation_of_table上创建了约束,但在插入时使用了revaluation_of_table_test

相关问题