This sounds like the prefect use case for the unique constraint.
class MyDomain {
String name
OtherDomain related
static constraints = {
name unique: ['related'] // each instance must have a unique name per related
}
}
Edit
Updated based on question in comment. The above will ensure that name is unique for each related. So, for example if MyDomain A has a related instance id of 1 and a name of "Test" no ohter instance of MyDomain with the same related instance can have the name of "Test". However, MyDomain B which has a rleated instance id of 2 can have a name of "Test" since the unique is per "related" in the above example.