【发布时间】:2021-09-28 16:58:16
【问题描述】:
我希望有人能帮我解决这个问题,或者如果我弄错了,可以提供替代方案。 我有两个表之间的多对多关系。
为了简单起见,假设有Events、Users、EventUsers,其中EventUsers 表有eventId、userId。
如何根据用户应明确为abc@test.com 和cde@test.com 的条件实现Events 表中的所有事件。
我尝试过这样做,但无济于事。
await Event.findAll({
include: [
{
model: User,
attributes: ['email'],
through: {
model: EventUsers,
attributes: [],
},
where: {
[Op.and]: [{ email: 'abc@test.com' }, { email: 'cde@test.com' }],
},
},
],
});
Event table
------------
id | title
------------
1 | lorem
2 | ipsum
User table
------------
id | email
------------
1 | abc@test.com
2 | cde@test.com
3 | efg@test.com
EventUsers table
------------
eventId| userId
------------
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
这必须给我一个 id 为 1 的事件,其中电子邮件是 abc@test.com 和 cde@test.com
【问题讨论】:
标签: javascript sql node.js postgresql sequelize.js