我的关系大致如下:
Parent: [id, name]
Children1: [id, parent_id, name]
Children2: [id, parent_id, name]
Children3: [id, parent_id, name]
Children4: [id, parent_id, name]
Parent
.hasMany -> Children1
.hasMany -> Children2
.hasMany -> Children3
.hasMany -> Children4
所以,如果我这样做:
Parent->findOne({
include: [{model: Children1}, {model: Children2}]
})
它只会将Parent带到children1和children2(即Inner join)。 如果我这样做:
Parent->findOne({
include: [
{model: Children1, required: false},
{model: Children2, required: false}
]
})
它将带来父母,如果有,它将带来Children1和/或Children2。 (即左连接)。
我想做的是如果存在Children1或Children2或ChildrenN,则只携带家长IF。可以是任何一个孩子N,也可以是所有孩子。只要至少有1个,我想带上父母。
有什么想法吗?
答案 0 :(得分:2)
将required: true
添加到您的包含对象,以使每个联接成为正确的联接。你有没有四个单独的子表?