0
votes

I would like to generate multicolumn unique constraints in Grails such as one defined in following entity class

class Relationship {
    Element source
    Element destination
    Type type

    // other properties omitted

    static constraints = {
         type unique: ['source', 'destination']
    }
}

but I don't want to have that constraint active during the validation as it consumes lot of resources (see alternative to grails multicolumn unique constraint (optimistic inserts)) for stats.

Is there any way to achieve that? What are the alternatives to generate the unique index automatically (with checking for existing one)?

1

1 Answers

0
votes

I've done this in an app where I knew that a column's values were unique based on how they were generated, so I didn't want Grails to run a select query during validation, but wanted the database check just in case something weird happened.

I would do this using a database migration. Add whatever constraints and indexes you like, in addition to the updates that are needed to keep the code and database in sync (e.g. adding/removing columns or tables, changing column types, etc.)