Sequelize:至少需要一个关联

时间:2017-10-18 19:01:52

标签: mysql node.js sequelize.js

我的关系大致如下:

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个,我想带上父母。

我复制了此question,因为情况相同。

有什么想法吗?

0 个答案:

没有答案