LoginSignup
2
2

More than 5 years have passed since last update.

[Grails]ユニーク制約(複数のキーで)

Posted at

複数のキーでユニーク制約を付ける

通常ユニーク制約を付けたいフィールドにname unique: trueとするだけだけど、複数のカラムを指定したい場合も当然ある。
例えば、商品名はユニークだけど、商品の種類が異なれば、商品名がかぶってもいい、など。
以下は、その例。

package sample

class Content {

    String name
    static belongsTo = [contentCategory: ContentCategory]
    Date dateCreated
    Date lastUpdated

    static constraints = {
        name unique: 'contentCategory'
    }
    static mapping = {
        version false
    }
}

結果的に "content_content_category_id_name_key" UNIQUE CONSTRAINT, btree (content_category_id, name)というユニーク制約が追加された。

参考

ttp://grails.jp/doc/latest/ref/Constraints/unique.html

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2