在多对多连接处进行排序 [英] Sequelize where on many-to-many join
问题描述
我希望有人能帮助我.我正在将Sequelize ORM用于express.js,并且我在2个表之间建立了多对多关系.
I am hoping somebody can help me. I am using the Sequelize ORM for express.js and I have a working many-to-many relationship between 2 tables.
为了简化查询,我们假设我的表是Users,Books和UserBooks,其中UserBooks具有UserId,BookId和一个附加列(假设其为NoOfTimesRead).
For the sake of simplifying my query lets pretend my tables are Users, Books and UserBooks where UserBooks has UserId, BookId and an additional column (Lets say its NoOfTimesRead).
我想做的事情是这样的:
What I want to do is something like:
user.getBooks({
where: {
"userBooks.NoOfTimesRead": 0
}
});
如果我输入:
user.getBooks();
这有效并返回所有书籍,我只是无法弄清楚如何通过联接表上的附加列来查询.
This works and returns all books, I just haven't been able to figure out how to query this by the additional column on the joining table.
我希望这是有道理的
感谢您的光临!
推荐答案
有了Belongs-to-Many,您可以根据直通关系和 选择特定的属性.例如,将findAll与through
With Belongs-To-Many you can query based on through relation and select specific attributes. For example using findAll with through
User.findAll({
include: [{
model: Project,
through: {
attributes: ['createdAt', 'startedAt', 'finishedAt']
where: {completed: true}
}
}]
});
基本上,您可以使用through
选项和include
链接的关系.可以在此处
Basically you can use through
option and include
the relationship that you linked. More can be found here
这篇关于在多对多连接处进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!