grails域约束映射到postgresql Gist约束

时间:2014-09-30 08:11:34

标签: postgresql grails

我有一个PostgreSQL表

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,
 ...
)

和一个约束,以防止在同一日期对同一财产进行2次预订

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

我可以在Grails预订域中强制执行SQL约束吗?

我正在考虑使用视图访问预留,以避免在groovy中使用postgresql范围出现问题

    create view resView as 
        select idReservation, 
            lower(start_end_dates) AS startDate,
            upper(start_end_dates) AS endDate,
            property_id
        from reservation_table

1 个答案:

答案 0 :(得分:0)

好的,但是您指定了解决此类问题的最低限度信息,因为我还没有完全理解,我有以下建议。

如何在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()
     }
  }

如果你需要更多帮助,请告诉我。 。 。