Search code examples
grailsgrails-orm

Is it possible to make nullable = true in a one to one relationship?


The two domains are as follows:

class Face {

    static hasOne = [nose:Nose]

    static constraints = {
        nose unique: true

    }

}   


class Nose {
    Face face

    static constraints = {

    }
}

The foreign key will be in the Nose table. The Nose table has reference to Face table. The face_id column in Nose table is not nullable. I was wondering if it is possible to make this column nullable. I tried the following constraint variations but none of them seem to alter the structure of the tables.

class Face {

    static hasOne = [nose:Nose]

    static constraints = {
        nose unique: true
        nose nullable: true

    }

}


I also tried the following 

class Nose {
    Face face

    static constraints = {
        face nullable:true
    }
}

I need a one to one relationship between these two domains but also want to be able to create independent Nose or Face entities or records. Is that possible? If so, can you suggest a way? Thanks! Appreciate it!


Solution

  • You can create a third relationship domain which would specify the relation between Face and Nose.

    FaceNose{
       Face face
       Nose nose
    
       static constraint{
          nose nullable:false,unique:true
          face nullable:false,unique:true
       }
    }
    
    
    class Face {
    //face class properties
        static constraints = {
    
        }
    
    }   
    
    
    class Nose {
        //nose class properties
        static constraints = {
    
        }
    }
    

    This fulfils your Need for one to one relationship with independent entries for both.