gpt4 book ai didi

mysql - Sequelize — 通过关联对象的精确数组查找?

转载 作者:行者123 更新时间:2023-11-29 12:47:10 24 4
gpt4 key购买 nike

假设我有 User 模型。它通过 UserFriends 表与 User 模型作为 Friends 进行多对多关联。

User = sequelize.define 'User',

name :
type : Sequelize.STRING
allowNull : false
defaultValue : ""

User
.hasMany User,
as : "Friends"
through : "UserFriends"

如何在好友中找到具有多个特定用户的 User 实例?例如,如果用户1有 friend 3,4,5,用户2有 friend 3,4,我正在搜索通过确切的 friend id数组[3,4]找到用户2的方法。

非常感谢!

最佳答案

User.find({
where: ...,
include: [{
model: User,
as: 'Friends',
where: { id: [3, 4] }
}]
})

如果您使用的是相当新的版本(很确定 2.0-dev12 有此功能),则在包含中添加 where 条件将创建一个内部联接,这意味着它只会返回用户有 ID 为 3 或 4 的好友

关于mysql - Sequelize — 通过关联对象的精确数组查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25281521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com