Grails 在一对一关系中是否可以使nullable=true?
这两个领域如下:Grails 在一对一关系中是否可以使nullable=true?,grails,gorm,Grails,Gorm,这两个领域如下: class Face { static hasOne = [nose:Nose] static constraints = { nose unique: true } } class Nose { Face face static constraints = { } } 外键将位于Nose表中。鼻梁台参照面部台。Nose表中的face_id列不可为空。我想知道是否有可能使这个列为空。我尝试了
class Face {
static hasOne = [nose:Nose]
static constraints = {
nose unique: true
}
}
class Nose {
Face face
static constraints = {
}
}
外键将位于Nose表中。鼻梁台参照面部台。Nose表中的face_id列不可为空。我想知道是否有可能使这个列为空。我尝试了以下约束变体,但它们似乎都没有改变表的结构
阶级面孔{
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
}
}
我需要这两个域之间的一对一关系,但也希望能够创建独立的鼻子或脸实体或记录。这可能吗?如果可能,你能建议一种方法吗?谢谢!非常感谢!你可以创建第三个关系域,指定脸和鼻子之间的关系
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 = {
}
}
这满足了您对一对一关系的需求,两个关系都有独立的条目。您可以创建第三个关系域,指定面部和鼻子之间的关系
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 = {
}
}
这就满足了您对一对一关系的需求,两个关系都有独立的条目。Grails使用hasOne关系创建“NOTNULL”外键
class Face{
..
static hasOne = [nose : Nose]
}
class Nose{
Face face
}
在nose表中创建“face_id”,并使用“not null”face_id
变通
class Face{
..
static hasMany = [noses: Nose]
static transients = ['getNose']
Nose getNose{
return this.noses[0] ?: null
}
}
face.nose//如果只有一个关系,Grails将创建“notnull”外键
class Face{
..
static hasOne = [nose : Nose]
}
class Nose{
Face face
}
在nose表中创建“face_id”,并使用“not null”face_id
变通
class Face{
..
static hasMany = [noses: Nose]
static transients = ['getNose']
Nose getNose{
return this.noses[0] ?: null
}
}
face.nose//这会起作用如果我们说鼻子在很多情况下是空的,那么它会打破限制。如果你需要那么高的灵活性,那么就去合成。如果我们说鼻子在很多情况下是空的,那么它会打破限制,那么你在一边把鼻子标记为唯一。去合成如果你需要高弹性。