简体   繁体   中英

Grails: How to limit the number of children in a one-to-many relationship

I want to restrict the number of children allowed in a one-to-many relationship between my Grails domain classes, preferably using constraints. The maxSize constraint seems to exist for this purpose (the Grails documentation confirms that it is supposed to work on collections as well as strings and arrays).

My constraint is defined as follows:

static constraints = {
    children(maxSize: 2)
}

However, during testing I found that Grails allows me to add as many children to the parent as I like, completely disregarding this constraint. Am I misunderstanding the documentation regarding the usage of this constraint? If so, is there another way to achieve this?

使用“大小”约束:

children(size: 0..2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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