问题
I have a relation which is roughly as follows:
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
So, if I do:
Parent->findOne({
include: [{model: Children1}, {model: Children2}]
})
It will only bring Parent where there's children1 and children2 (ie, Inner join). If I do:
Parent->findOne({
include: [
{model: Children1, required: false},
{model: Children2, required: false}
]
})
It will bring Parent and if there's it will bring Children1 and/or Children2. (ie Left join).
What I want to do is to bring Parent IF AND ONLY IF either Children1 or Children2 or ChildrenN exists. Could be any of the ChildrenN or could be all of them. As long as there's at least 1, I want to bring Parent.
Any ideas?
回答1:
Add required: true to your include objects to make each join a right join. Any reason you've got four separate children tables?
来源:https://stackoverflow.com/questions/38504835/sequelize-parent-with-at-least-one-children