Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a PostgreSQL table

CREATE  TABLE reservation_table (
  idreservation         SERIAL NOT NULL,
  entry_datetime        TIMESTAMPTZ NOT NULL DEFAULT 'NOW',
  start_end_dates       DATERANGE NOT NULL ,
  property_id           INT NOT NULL REFERENCES property_table,
 ...
)

and a constraint to prevent 2 reservations of the same property on same date

ALTER TABLE ONLY reservation_table
  ADD CONSTRAINT reservation_double_booking_constraint
  EXCLUDE USING gist
    (property_id WITH =, start_end_dates WITH &&)
 ;

Can I enforce my SQL constraint within my Grails Reservation domain ?

I am considering accessing reservation using a view, to avoid problems with postgresql range in groovy

    create view resView as 
        select idReservation, 
            lower(start_end_dates) AS startDate,
            upper(start_end_dates) AS endDate,
            property_id
        from reservation_table
share|improve this question
    
you mean you want to map your domain with view than table ? – danielad Oct 4 '14 at 11:30
    
I want a simple, easy to maintain domain. The range type is not fully supported yet - so using a view with a start date and end date simplifies my grails code while allowing my DB constraint to prevent double booked apartments entered via another app. This is my first web app, so I'm still learning what hibernate/grails constraint can & cannot do. I was hoping someone could point me toward a GIST style constraint mechanism within grails/hibernate - but I have not found that. – Alex Cornford Oct 5 '14 at 11:38
up vote 0 down vote accepted

Ok , but you specified very minimum information to solve this kind of problem , since i have not understood it fully , i have the following suggestion.

How about using before and after interceptor on grails.

 class Reservation {

     Date startDate ...

    def beforeInterceptor = {

    //do some date validation here start data or end date
    //for exmaple you might check that we have 
    //any start date currently existing on db
    //by findbyStartDate or endDate
    //then reject the reservation or saving of this model 
        println "validation error here , cancel save"
    }
    static constraints = {
      //do some validation here related to ur constraint
      startDate max: new Date()
     }
  }

let me know anything if u need more help . . .

share|improve this answer
    
Postgresql daterange holds start,end date. The postgresql constraint prevents a reservation insert where the range overlaps with an existing reservation (for same property). Am looking for a groovy constraint - not async code. – Alex Cornford Sep 30 '14 at 10:07
    
github.com/kaleidos/grails-postgresql-extensions postgresql ranges are still in development. I'm going to implement a simple reservation domain based on the view with a standard start and end date. The domain constraint repeats the database constraint, violating DRY (don't repeat yourself) – Alex Cornford Oct 1 '14 at 12:17
    
dude am glad it worked for you !! – danielad Oct 6 '14 at 14:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.