简体   繁体   English

Grails域类约束'Not Null'

[英]Grails Domain class constraint 'Not Null'

I have a domain class: 我有一个域类:

class ProgramArea {
       String        programCd
       String        programArea
       String        description

    static constraints = {
programCd(nullable: false, unique: true)
              programArea(nullable: false, blank: false, unique: true)
              description(nullable: true, blank: true, maxSize: 50)
    }

}

In the database, I want the programCd field to be a 'NOT NULL' field. 在数据库中,我希望programCd字段为“ NOT NULL”字段。 However, this is not user entered field. 但是,这不是用户输入的字段。 I have code in the controller to generate programCd. 我在控制器中有代码来生成programCd。 However, when I try to insert new data, I get a validation error saying programCd cannot be null. 但是,当我尝试插入新数据时,出现验证错误,提示programCd不能为null。

If I modify the constraint for programCd as 'nullable: true', everything works fine. 如果我将programCd的约束修改为“ nullable:true”,则一切正常。 Can someone please let me know if there is a workaround for this? 有人可以让我知道是否有解决方法吗?

Please make sure that you are assigning the value of "programCd" to the object of the class. 请确保将“ programCd”的值分配给该类的对象。 What seems to be an error here is that you might be generating the value of programCD but might have not added to the objec of the class, thus this value remains null. 这里似乎是错误的是,您可能正在生成programCD的值,但可能没有将其添加到类的objec中,因此该值保持为空。

Try checking your controller or paste the code here, so that we can help 尝试检查您的控制器或将代码粘贴到此处,以便我们提供帮助

You may have to revalidate your domain instance after modifying it: 修改域实例后,您可能必须重新验证它:

programAreaInstance.programCd = 'something'
programAreaInstance.validate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM