[英]Sequelize Or condition in Join
I'm trying to do a query with Sequelize ORM with custom join condition.我正在尝试使用自定义连接条件对 Sequelize ORM 进行查询。
There is the example:有一个例子:
User.findAll({include: [{model: Post, where: {active: true}}]
This is the result of join condition:这是连接条件的结果:
INNER JOIN `posts` AS `post` ON `users`.`id` = `post`.`user_id` AND `post`.`active`=true
What I'm trying to do is replace the AND condition to OR and spect something like this:我想要做的是将AND条件替换为OR并观察如下内容:
INNER JOIN `posts` AS `post` ON `users`.`id` = `post`.`user_id` OR `post`.`active`=true
I don´t know if there is a way to do that.我不知道是否有办法做到这一点。
You can't add conditions to a JOIN condition with OR.您不能使用 OR 将条件添加到 JOIN 条件。 You can only add additional outside conditions among which you can use OR.
您只能添加可以使用 OR 的其他外部条件。
User.findAll({
where: {
[Op.or]: [
{ '$Post.somefield$': sequelize.col('some_user_field') },
{ '$Post.someotherfield$': sequelize.col('some_other_user_field') }
]
},
include: [{
model: Post
}
]})
This query will be like this in SQL:这个查询在 SQL 中会是这样的:
SELECT * FROM USERS as `users`
INNER JOIN `posts` AS `post` ON `users`.`id` = `post`.`user_id`
WHERE
`post`.`somefield`=`users`.`some_user_field`
OR
`post`.`someotherfield`=`users`.`some_other_user_field`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.