Re: OVERLAPS constraint using TIME columns

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Gio - <gio-force(dot)2(dot)1(at)hotmail(dot)com>, "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: OVERLAPS constraint using TIME columns
Date: 2018-02-22 08:58:44
Message-ID: 1519289924.2587.2.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Gio - wrote:
> I have a table for weekly time slots with columns day, from, to.
>
> I would like to add a constraint so that overlapping time slots cannot be added to the db.
>
> The OVERLAPS operator works as I need to, eg
> SELECT (TIME ‘5:00', TIME '10:00') OVERLAPS (TIME '22:59', TIME '23:10');
>
> But I can’t use it inside a constraint (ERROR: syntax error near “,”)
>
> ALTER TABLE slots
> ADD CONSTRAINT same_day_slots_overlap
> EXCLUDE USING GIST
> (
> day WITH =,
> (from, to) WITH OVERLAPS
> );
>
> Same error happens if I use the && operator.
>
> I only need time information in my columns so I can either model them as TIME or INTEGER (as minutes from the start of the day). How can I add
> such a constraint with these columns?

What about

ALTER TABLE slots
ADD CONSTRAINT same_day_slots_overlap
EXCLUDE USING gist (tsrange(day + "from", day + "to") WITH &&);

It is strange to store date and time separately.
That way you cannot use "timestamp with time zone", which is almost always
the correct data type to use.

Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Gio - 2018-02-22 09:03:59 Re: OVERLAPS constraint using TIME columns
Previous Message Gio - 2018-02-21 21:21:27 OVERLAPS constraint using TIME columns