Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

sequelize如何让多对多查询返回的某一属性是数组?

在多对多情况下,通过中间表来连接两表(人物表、业务表),一个人物会有多个业务,通过belongsToMany through中间表来连接。
现在查询的时候,include模块之后返回的数据一个人包含多条,而每一条只有一个业务。这种情况下,会导致分页查询计算(findAndCountAll)的时候返回的count是比原来多的,请问有什解决么方案吗


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
// 关系
BusinessModel.belongsToMany(UserModel, { as: 'businesses', through: UserBusiness })

// 查询
UserModel.findAndCountAll({
  include: [
    {
      model: BusinessModel,
      as: 'businesses'
    }
  ]
})

简单给你个参考,如果有帮助的话请点个采纳点个赞,谢谢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...