【问题标题】:Postgres conditional insert with an ON CONFLICT clause带有 ON CONFLICT 子句的 Postgres 条件插入
【发布时间】:2020-11-18 10:19:30
【问题描述】:

我有下表:

CREATE TABLE orders(
     order_id text primary key,
     payment_status text not null
)

如果付款状态发生更改,我想在其中更新它:

INSERT INTO orders(
        order_id, 
        payment_status
)
VALUES(
        '101',
        'Paid'
)
ON CONFLICT (order_id) DO UPDATE SET
        payment_status = EXCLUDED.payment_status

有三种可能的付款状态:

  • 等待付款
  • 付费
  • 已退款

ON CONFLICT 子句用于在订单行从“已付款”更改为“已退款”时更新订单行。但是,该表应该只包含状态为“已付款”或“已退款”的订单行,而不是“等待付款”的订单行。如果我在语句末尾指定 WHERE 条件为

WHERE EXCLUDED.payment_status != 'Awaiting payment'

此 where 条件仅在主键发生冲突时适用,而不是在插入新的 order_id 时适用,从而让处于“等待付款”状态的订单在第一次插入时传递。

如何进行这样的插入,只允许在第一次插入时插入 payment_status 的某些值?

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    该表应仅包含状态为“已付款”或“已退款”的订单行,而不是“等待付款”。

    CHECK constraint 是解决此问题的正确方法,并且也不限于特定查询。

    CREATE TABLE orders(
         order_id text primary key,
         payment_status text not null CHECK (payment_status IN ('Paid', 'Refunded'))
    )
    

    【讨论】:

      猜你喜欢
      • 2017-11-18
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 2021-11-07
      • 2020-12-07
      • 1970-01-01
      • 2020-04-09
      • 2020-06-02
      相关资源
      最近更新 更多