我有下表:
create table booking (
identifier integer not null primary key,
room uuid not null,
start_time time without time zone not null,
end_time time without time zone not null
);
我想创建一个exclude constraint
来强制同一房间没有重叠预约。
我尝试了以下方法:
alter table booking add constraint overlapping_times
exclude using gist
(
cast(room as text) with =,
period(start_time, end_time) with &&)
);
这有两个问题:
将
room
转换为text
是不够的,它会给出:错误:数据类型 text 没有访问方法“gist”的默认运算符类
。我知道在 v10 中有btree_gist
,但我使用的是 v9.5 和 v9.6,所以我必须手动将uuid
转换为text
afaik。period(...)
是错误的,但我不知道如何构造time without time zone
类型的范围。
最佳答案
在 installing 之后btree_gist ,您可以执行以下操作:
create type timerange as range (subtype = time);
alter table booking add constraint overlapping_times
exclude using gist
(
(room::text) with =,
timerange(start_time, end_time) with &&
);
如果您想在约束中使用表达式,则需要将其放入括号中。所以 (room::text)
或 (cast(room as text))
关于sql - Postgres - 'time without time zone' 范围和排除约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47120170/