【问题标题】:Grails domain class: unique constraint for multiple columnsGrails域类:多列的唯一约束
【发布时间】:2011-11-26 20:34:06
【问题描述】:

假设一个简单的 Grails 域类:

class Account {
    String countryId;

    String userName;

    String password;

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

对于特定的countryId,用户名要求是唯一的,因此两列必须有一个unique 约束。如何在 constraints 定义中表达这一点?

【问题讨论】:

  • 我正在浏览网页,尤其是 stackoverflow,但没有找到答案。最后,我在Grails reference 中找到了解决方案,尽管所有其他 Grails 文档示例仅包含单列案例。不知道8小时内不能自答

标签: grails orm unique-constraint grails-domain-class grails-validation


【解决方案1】:
userName(unique: ['countryId'])

您可以在数组中包含尽可能多的其他属性,这些属性构成在用户名的“唯一”约束中必须考虑的其他属性。

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
相关资源
最近更新 更多