开发者

Grails domain ID column validation issue

开发者 https://www.devze.com 2023-04-08 23:14 出处:网络
I have a simple domain object class MyDomain { String id String name static constraints = { id unique:true name nullable:true

I have a simple domain object

class MyDomain
{
  String id
  String name

  static constraints = 
  {
     id unique:true
     name nullable:true
  }

 static mapping = 
 {
   table 'schema.MyDomain'
   id column:'MY_ID', type:'string', generator:'assigned'
 }
}

The issue I am having is when I call validate on the object, it returns true even when the id field is null. I had thoug开发者_C百科ht that all columns were nullable:false unless explicitly stated otherwise. If I change the line

id unique:true

to

id unique:true, nullable:false

then it seems to work fine. My main question is, why do I have to explicitly set nullable for the ID column? It is just a small line of code, but I don't like just adding in the tag of code without understanding why in case it is a symptom of a bigger problem.


The id column is auto generated and auto populated(when versioning is turned on[true by default]) and you shouldn't have to declare a new one. This default id column is nullable:false by default and you can still set the mapping properties and id generation strategies like you have done above against it.

However if you want to define the default constraints for all domain in you app, you can do it globally by setting thwe following in your config.groovy file.

grails.gorm.default.constraints = {
   myShared(nullable:true, size:1..20)
}

For more on constraints see the Grails documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号