0

我有一个名为模的一些性质,并用的方法的控制器,从模型中创建一个对象,并将其保存域,当执行保存shell显示此错误:的Grails:房产零误差

  • 拉propiedad [{0}] de la clase [{1}]没有puede ser nulo

但是,如果我将约束设置为空,错误再次显示。我认为我不应该设定这个合约。

该模型链接到一个mysql表,其中包含所有属性,但id为null。

我想我在这里没有做错什么。任何建议?

域:模

class Modulo { 
    String nombre 
    String icon 
    String url 




//static constraint = { 
    // url(nullable:true) 
    //} 

    } 

控制器:实施例

class ExampleController { 
    def index = { 
     def modulo = new Modulo(
     nombre:'xxx', 
     icon:'xxx' 
     ) 

     if (modulo.save()){ 
      println 'ok' 
     }else{ 
      modulo.errors.allErrors.each { println it.defaultMessage} 
     } 
    } 
} 

感谢。 José

+0

Jose,默认情况下,可以将可空约束设置为false,我看到你的约束在你的问题中评论过,这是真的吗?你有url的可空约束设置为true?因为如果没有设置,那么保存时就会出现错误。 – Maricel 2011-03-17 21:35:01

+0

您是否尝试过'grails clean'? – 2011-03-18 10:08:35

+0

好的。我执行grails干净,但错误继续。 – richardhell 2011-03-18 23:40:56

回答

2

最后我解决了错误。这不会保存,因为约束写错了。

错误代码

static constraint = { 
    url(nullable:true) 
} 

优良程序代码

static constraints = { 
    url(nullable:true) 
} 

感谢所有为你解答。 :P

+0

我很高兴你发现错误:-) – Maricel 2011-03-19 00:16:46

0
But if i set the constraint nullable to true, the error show 

again. I think that i should not set this cosntraint.

可空约束适用于我。如果你没有指定它,你肯定会得到错误,因为默认情况下nullable = false。

请尝试打开nullabe:true,并重新启动Grails。

The model is linked to a mysql table with all properties except id allow null.

其实,你并不需要预先定义的表,但如果这样做,我认为这是最好的,不允许在ID字段为空。

+0

Hello @HoàngLong我将约束nullabe设置为true,然后重新启动服务器,但错误继续显示。 – richardhell 2011-03-18 23:40:23