Sequelize: Parent with at least one children

一个人想着一个人 提交于 2020-01-13 16:25:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!