如果没有这个限制,Grails会在字段上抛出一个不能为空的错误

时间:2014-04-08 18:38:31

标签: mysql grails gorm triggers

使用GRAIL和MySQL构建我的应用程序。

我有一个MySQL BEFORE INSERT触发器,效果非常好:

USE `scheduler`;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` TRIGGER `devices_BINS` BEFORE INSERT ON `devices` FOR EACH ROW
begin
    If new.device_type = 'DU' THEN
        Select IFNULL(max(external_id), '00-000') into @oldMc from `devices` where device_type = 'DU';
        SET @IntMc = CAST(Concat(left(@oldMc,2), right(@oldMc,3)) as unsigned);
        SET @IntMc=@IntMc+1;
        set @newMC = LPAD(CAST(@IntMc as CHAR(10)), 5, 0);
        set new.external_id = CONCAT(left(@newMC, 2),'-',right(@newMC, 3));
    elseiF new.device_type = 'ATM' THEN
        set new.external_id = new.serial_number;
    END IF;     
end

当我在GRAILS域中实现结构时,尝试创建一个条目,我得到:

Property [external_id] of class [class cms.Devices] cannot be null

来自对象的Errors属性。

这是奇怪的事情:在DB中允许external_id为空(甚至将Null作为默认值);并且域中唯一的约束是external_id(空格:true)。顺便说一句:改变这似乎没有效果。

有什么想法?或者我怎么可能只是“跳过”控制器验证检查中的错误?

这是域类

class Devices {
    String external_id
    String serial_number
    String model_type
    Device_Type device_type
    Status status
    String toString(){
        "$device_type - $external_id"
    }   

    enum Status {
        Tentative, Active, Offline, Terminated
    }

    enum Device_Type {
        DU, ATM
    }

    Locations locations

    static constraints = {
        device_type()
        model_type()
        serial_number(unique: true)
        status()
        external_id(blank:true)
    }

    static mapping = {
        version false
        id column: 'device_id'
        locations column:'location_id'
    }
}

0 个答案:

没有答案