Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exclude overlapping events (start/end) while considering the venue_id

I have the following table:

events
- id
- venue_id
- starts_at
- ends_at

I found this constraint https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-EXCLUDE but I’m not sure I understand how I can use it to prevent the creation of overlapping events while considering the venue_id so that there can be overlapping events, but not for the same venues.

for example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EXCLUDE USING gist (tsrange(starts_at, ends_at) and somehow consider venue_id

Thanks

>Solution :

You need to combine the "overlapping" of the timestamp range with an = operator on the venue_id

alter table events 
   add constraint no_overlapping_events
   exclude using gist (venue_id with =, tsrange(starts_at, ends_at) with &&)

Note that this requires the bree_gist extension due to the use of the = operator

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading