有很多通过 - 通过关联 Sequlize 关联

时间:2021-04-12 13:06:39

标签: javascript node.js sequelize.js

我有以下型号:

  • Users haveMany Posts
  • Posts haveMany Comments

使用 Sequelize 有没有办法使用关系获取给定 Comments 的所有 User?而不是自定义查询?

我正在寻找:

Users hasMany Commentsmodels.Posts 或其他东西,但没有“连接器”表,如 belongsToMany

我希望能够使用以下代码:

const userId = 123;
const user = User.findOne({
    where: {id: userId}, 
    include: [{ model: models.Comment }]
});
const comments = user.comments;
console.log(comments); // Array of Comment objects

我在 SO 上发现了一个 duplicate question,但它已经有 4 年历史了,我希望有些东西可能已经改变了。

0 个答案:

没有答案