Search code examples
grailsgrails-orm

Why is unique good practise in 1:1 relationships?


In the Grails Doc: http://grails.org/doc/2.3.x/guide/GORM.html It says that adding a unique constraint in a 1:1 is good practise.
So if a face has one nose, we should do:

class Face {
    static hasOne = [nose:Nose]
    static constraints = {
        nose unique: true
    }
}

But Why? Surely, the constraint is implicit in the cardinality?

So why should we do it?


Solution

  • The reason for putting the unique constraint is to ensure that two faces don't have the same nose. Since this is a 1:1 relationship the Id of the nose is kept on the face. That's why.