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