Sergey Ponomarev
Sergey Ponomarev

Reputation: 3191

Grails: How can I create named unique constraint for multiple columns?

How can I create named unique constraint for multiple columns?

I've three classes:

class Descriptor {
    // some columns
}

class Protein {
    // some columns
}

class DescriptorValue {
    // some columns
    static belongsTo = [protein: Protein, descriptor: Descriptor]
    static constraints = {
        protein(unique:['descriptor'])
    }
}

GORM creates an index with an auto generated name that is different for different environments. How can I specify its name?

Upvotes: 4

Views: 2831

Answers (2)

Zhuravskiy Vitaliy
Zhuravskiy Vitaliy

Reputation: 89

String field1
String field2
Integer field3
SomeObject object

  static constraints = {
        object unique: ['field1','field2', 'field3']
    }

Upvotes: 0

Tiago Farias
Tiago Farias

Reputation: 3407

Try to do it like:

static mapping = {
    protein unique:['descriptor'], index: 'protein_idx' //or whatever name you like
}

If you need to use multi-column indices, then you can specify for every property the same index name.

Upvotes: 1

Related Questions