[英]Is there a way to include an array in a where: $or clause with sequelize?
I have a array of Ids teamsIdsSelected = ['1', '5', .., 'X']
我有一组 Ids
teamsIdsSelected = ['1', '5', .., 'X']
Then, from my table named 'Challenge' I want to get all the the challenges that are linked to each teamId from the array teamsIdsSelected.然后,从名为“挑战”的表中,我想从数组 teamIdsSelected 中获取与每个 teamId 相关联的所有挑战。 So far, this is what I tried:
到目前为止,这是我尝试过的:
createWhereClause(column, field) {
return models.sequelize.where(
models.sequelize.literal(`${column} in (`),
`'${field}'`,
models.sequelize.literal(')'))
},
const challenges = await Challenge.findAndCountAll({
where: {
team_id: createWhereClause(team_id, teamsIdsSelected)
}
})
But it doesn't work, I only obtain the challenge with team_id equal to the last item of teamsIdsSelected = teamsIdsSelected.slice(-1)[0]
For instance if teamsIdsSelected = ['1', '4', '7']
, the where clause at the end (when I look at the log) is only "where challenge.team_id = 7"
.但它不起作用,我只获得 team_id 等于
teamsIdsSelected = teamsIdsSelected.slice(-1)[0]
的最后一项的挑战,例如如果teamsIdsSelected = ['1', '4', '7']
,最后的 where 子句(当我查看日志时)只是"where challenge.team_id = 7"
。
What I would actually like is an OR condition that would take all challenges that match either 1, 4, or 7 as a team_id.我真正想要的是一个 OR 条件,它将所有与 1、4 或 7 匹配的挑战作为 team_id。 Is there a fast way to do it without creating a for loop ?
有没有一种快速的方法可以在不创建 for 循环的情况下做到这一点?
Let me know if you need more precision about my issue.如果您需要更精确地了解我的问题,请告诉我。 Thanks.
谢谢。
const challenges = await Challenge.findAndCountAll({
where: {
team_id: { [Op.in]: teamsIdsSelected }
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.