本文介绍了Grails 域类:多列的唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个简单的 Grails 域类:

Suppose a simple Grails domain class:

class Account {
    String countryId;

    String userName;

    String password;

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

要求用户名对于特定的 countryId 是唯一的,因此必须对两列有 unique 约束.如何在 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?

推荐答案

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.

因此,例如,如果您想让 userNamecountryIdprovinceId 中唯一,它看起来像这样:

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

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

这篇关于Grails 域类:多列的唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:29
查看更多