在考虑 venue_id 的同时排除重叠事件 (start/end)
Exclude overlapping events (start/end) while considering the venue_id
我有以下 table:
events
- id
- venue_id
- starts_at
- ends_at
我发现了这个约束 https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-EXCLUDE 但我不确定我是否理解在考虑 venue_id
时如何使用它来防止重叠事件的创建,以便可以有重叠事件,但不适用于相同的场地。
例如:
EXCLUDE USING gist (tsrange(starts_at, ends_at)
并以某种方式考虑 venue_id
谢谢
您需要在 venue_id
上将时间戳范围的“重叠”与 =
运算符结合起来
alter table events
add constraint no_overlapping_events
exclude using gist (venue_id with =, tsrange(starts_at, ends_at) with &&)
请注意,由于使用了 =
运算符
,因此需要 bree_gist 扩展
我有以下 table:
events
- id
- venue_id
- starts_at
- ends_at
我发现了这个约束 https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-EXCLUDE 但我不确定我是否理解在考虑 venue_id
时如何使用它来防止重叠事件的创建,以便可以有重叠事件,但不适用于相同的场地。
例如:
EXCLUDE USING gist (tsrange(starts_at, ends_at)
并以某种方式考虑 venue_id
谢谢
您需要在 venue_id
=
运算符结合起来
alter table events
add constraint no_overlapping_events
exclude using gist (venue_id with =, tsrange(starts_at, ends_at) with &&)
请注意,由于使用了 =
运算符