ethaler
ethaler

Reputation: 165

Unique constraints by subclass

I currently have a set of domain subclasses that are mapped via table-per-hierarchy. One of the attributes in the base class needs to be unique but only within each subclass. My initial thought was to use the discriminator to create a multi-column unique constraint, something like this:

BaseClass {
   String name

   static constraints = {
       name unique 'discriminator'
   }
}

I get back the error:

Scope for constraint [unique] of property [name] of class [class BaseClass] must be a valid property name of same class

Is there a way to use the discriminator for this purpose or another way to go about setting a property unique per subclass? Switching to table-per-subclass is not really on the table.

Upvotes: 5

Views: 203

Answers (1)

ethaler
ethaler

Reputation: 165

Well that was a harrowing couple of hours. The above code was nearly right, just needed to be in each subclass and use the column name of the discriminator, which defaults to 'class'.

SubClass extends BaseClass {
    static constraints = {
        name unique: 'class'
    }
}

Upvotes: 2

Related Questions