【发布时间】:2021-09-28 09:56:18
【问题描述】:
我有一个表格来跟踪带有排除约束的酒店预订,如下所示。
目前,我允许客人更新他们的预订。因此,如果 guest_id 1 将他的预订从 3 天预订更改为从 2010-01-03、2010-01-03 开始的一天,如果我运行此更新语句,postgres 将由于重叠约束而阻止更新:
update reservation set from_ts = '2021-01-03 00:00:00', to_ts='2021-01-10 23:59:00', during = '[2021-01-03 00:00:00, 2021-01-10 23:59:00]' where id = 1
那么您如何允许此更新?您是否必须保持预订 id 相同,并删除其他的?
** 注意:我实际上是每行存储一天,因为我还有其他属性可以每天跟踪**
Table: reservation
id | room | from_ts | to_ts | guest_id
----+------+---------------------+---------------------+------------
1 | 101 | 2010-01-01 00:00:00 | 2010-01-01 23:59:00 | 1
2 | 101 | 2010-01-02 00:00:00 | 2010-01-02 23:59:00 | 1
3 | 101 | 2010-01-03 00:00:00 | 2010-01-03 23:59:00 | 1
CREATE TABLE reservation (
id int,
guest_id int,
room int,
from_ts timestamp without time zone,
to_ts timestamp without time zone,
during tsrange,
EXCLUDE USING GIST (room WITH =, during WITH &&)
);
-- bootstrap to test the problem
INSERT INTO reservation ( id, guest_id, room, from_ts, to_ts, during ) VALUES ( 1, 1, 101, '2021-01-01 00:00:00', '2021-01-01 23:59:00', '[2021-01-01 00:00:00, 2021-01-01 23:59:00]');
INSERT INTO reservation ( id, guest_id, room, from_ts, to_ts, during ) VALUES ( 2, 1, 101, '2021-01-02 00:00:00', '2021-01-02 23:59:00', '[2021-01-02 00:00:00, 2021-01-02 23:59:00]' );
INSERT INTO reservation ( id, guest_id, room, from_ts, to_ts, during ) VALUES ( 3, 1, 101, '2021-01-03 00:00:00', '2021-01-03 23:59:00', '[2021-01-03 00:00:00, 2021-01-03 23:59:00]' );
-- update statement will fail after you run the insert statements
update reservation set from_ts = '2021-01-03 00:00:00', to_ts='2021-01-10 23:59:00', during = '[2021-01-03 00:00:00, 2021-01-10 23:59:00]' where id = 1
【问题讨论】:
-
不,不会。只要确保你使用
UPDATE,而不是INSERT。 -
我明白了。这样做是否可以将guest_id包含在这样的要点中?排除使用 GIST (guest_id with , room WITH =, tsrange(from_ts,to_ts) WITH &&)
-
不,不要添加客人。
-
我更新了我的问题,使其更接近于我存储数据的方式。
-
我用插入语句更新了这个问题,您可以运行这些语句来重现它。 create table 语句应该可以工作。实际上,我仍在探索这一点。我认为这是明确的方式。您是否建议将“during”之类的显式字段与排除约束一起使用?
标签: postgresql exclusion-constraint