Sequelize where on many-to-many join(续集多对多连接的位置)
问题描述
我希望有人可以帮助我.我正在为 express.js 使用 Sequelize ORM,并且我在 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.
为了简化我的查询,假设我的表是用户、书籍和用户书籍,其中用户书籍有用户 ID、书籍 ID 和一个附加列(假设它的 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).
我想做的是这样的:
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
这篇关于续集多对多连接的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:续集多对多连接的位置


基础教程推荐
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01