是否可以在一对一关系中使 nullable = true?

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

两个域名如下:

class Face {

    static hasOne = [nose:Nose]

    static constraints = {
        nose unique: true

    }

}   


class Nose {
    Face face

    static constraints = {

    }
}

外键会在鼻子里table。鼻子 table 引用了面部 table。 Nose table 中的 face_id 列不可为空。我想知道是否有可能使此列可为空。我尝试了以下约束变体,但其中 none 似乎改变了 table 的结构。

class 脸 {

    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
    }
}

我需要这两个域之间的一对一关系,但也希望能够创建独立的 Nose 或 Face 实体或记录。那可能吗?如果是这样,你能建议一种方法吗?谢谢!点赞!

您可以创建第三个关系域来指定面部和鼻子之间的关系。

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 = {

    }
}

这满足了您对一对一关系的需求,两者都有独立条目。

对于 hasOne 关系,Grails 创建 "not null" 个外键。

class Face{
     ..
     static hasOne = [nose : Nose]
}

class Nose{
     Face face
}

在鼻子 table 和 "not null" face_id 中创建 "face_id"。

解决方法

class Face{
     ..
     static hasMany = [noses: Nose]
     static transients = ['getNose']

     Nose getNose{
         return this.noses[0] ?: null 
     }
}

face.nose // 这行得通