Grails domain class: unique constraint for multiple columns

扶醉桌前 提交于 2019-11-27 12:40:34

问题


Suppose a simple Grails domain class:

class Account {
    String countryId;

    String userName;

    String password;

    static constraints = {
        ...???...
    }
}

It is required that user names are unique for a particular countryId, thus there must be a unique contraint on two columns. How to express this in the constraints definition?


回答1:


userName(unique: ['countryId'])

You can include as many other properties in the array that make up the other properties that must be considered in the "unique" constraint on the username.

So, for example if you wanted to make userName unique within a countryId and provinceId it would look like this:

userName(unique: ['countryId', 'provinceId']


来源:https://stackoverflow.com/questions/7583380/grails-domain-class-unique-constraint-for-multiple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!