Sequelize: Parent with at least one children(续集:父母至少有一个孩子)
问题描述
我的关系大致如下:
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).如果我这样做:
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}
]
})
它会带来 Parent,如果有的话,它会带来 Children1 和/或 Children2.(即左连接).
It will bring Parent and if there's it will bring Children1 and/or Children2. (ie Left join).
我想要做的是在且仅当 Children1 或 Children2 或 ChildrenN 存在时才带上父母.可以是任何一个 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?
这篇关于续集:父母至少有一个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:续集:父母至少有一个孩子
基础教程推荐
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
