续集:有至少一个孩子的父母 [英] Sequelize: Parent with at least one children

查看:71
本文介绍了续集:有至少一个孩子的父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的关系大致如下:

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

它只会将父级带到有子级1和子级2的位置(即内部联接). 如果我这样做:

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

它将带父母,如果有,它将带子1和/或子2. (即左联接).

It will bring Parent and if there's it will bring Children1 and/or Children2. (ie Left join).

我想做的是带上父母",并且只有"Children1"或"Children2"或"ChildrenN"存在时才带.可以是ChildrenN中的任何一个,也可以是全部.只要至少有1个,我想带上父母.

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.

有什么想法吗?

推荐答案

required: true添加到您的包含对象中,以使每个联接成为正确的联接.有任何理由要有四个单独的子表?

Add required: true to your include objects to make each join a right join. Any reason you've got four separate children tables?

这篇关于续集:有至少一个孩子的父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆