带有包含和约束的 NodeJS Sequelize 和 FindAll [英] NodeJS Sequelize and FindAll with Include and constraints

查看:26
本文介绍了带有包含和约束的 NodeJS Sequelize 和 FindAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Find all 中的 Include 功能,以便在单个查询中加载我需要的数据.我的问题是所有包含的内容都是 LEFT OUTER JOIN ... 这很好,但我没有看到向该 LEFT OUTER JOIN 添加额外约束的方法.

I am trying to use the Include capabilities in the Find all to Eager load the data i need in a single query. My issue is that all the includes are LEFT OUTER JOIN ... which is good but i am not seeing a way to add additional constraints to that LEFT OUTER JOIN.

我的查询类似于:

Matches.findAll(
   {where: ["match.isPublished = ?", true], 
    include: [RoundMaps, 
              Locations, 
              {model: MemberMaps, as: "MemberMaps", where: ["memberMaps.memberUUID = ?", authenticatedUser.uuid]}]})

但这似乎不受支持......至少我编写语法的方式是这样.我无法将该约束添加到 LEFT OUTER JOIN 上的约束......向 WHERE 子句添加相同的约束不起作用......因为这仅给了我与成员关联的匹配项.我想要所有公开比赛的列表,并知道会员已经建立了哪些比赛.

But this does not seem to be supported ... at least the way i have the syntax written. I cannot add that constraint to the constraints on the LEFT OUTER JOIN ... adding the same constraint to the WHERE clause does not work ... as that gives me only the Matches that the Member is associated with. I want the list of all public Matches and to know which of those that the member has already established an association.

能做到吗?Sequelize 似乎完成了大部分事情!但仍然在一些事情上挣扎.

Can it be done? Sequelize seems to be getting most things done! But still struggling with some things.

欢迎提供见解!

干杯

推荐答案

INCLUDE 上有一个选项可以指定连接是内部连接还是外部连接:include.require = boolean

There is an option on INCLUDE that specifies whether the join will be inner or outer: include.require = boolean

如果没有指定 where: 子句,则默认为 false.但是,当设置了 where: 时,它会设置为 true.

When there isn't a where: clause specified, then it defaults to false. But, it gets set to true when a where: is set.

您可以显式关闭它以获取 findAll() 的所有结果.

You can explicitly turn that off to fetch all of the results of the findAll().

不幸的是,我见过的唯一例子是在代码库中,但这里是:sequelize/test/integration/associations/belongs-to-many.test.js#L167

Unfortunately, the only examples I've seen of this are in the codebase itself, but here it is: sequelize/test/integration/associations/belongs-to-many.test.js#L167

这篇关于带有包含和约束的 NodeJS Sequelize 和 FindAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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