Reputation: 2322
Assuming i have the following grail domain entities:
class A { ... }
class B { ... }
In a third entity i have a one to many relationship as follows:
class C {
static belongsTo = [a: A, b: B]
static constraints {
a unique: 'b'
}
}
Is it possible in grails to define a unique relationship based on both properties (a
and b
) in class C
, so no two C
instances may be created having the same combination of a
and b
?
EDIT: My test case has as follows:
void testCompositeUniqueConstraint() {
A a = // ...
B b = // ...
C existing = // ...
existing.a = a
existing.b = b
mockForConstraintsTests(C, [existing])
C c = // ...
c.a = a
c.b = b
assertFalse c.validate()
}
On my test class i have included the @Mock([A, B])
annotation. I would expect this test to fail but it passes.
Upvotes: 0
Views: 364
Reputation: 75671
That'd be
static constraints = {
a unique: 'b'
}
See http://grails.org/doc/latest/ref/Constraints/unique.html
Upvotes: 1