Sequelize 从外键获取 N:M 关联

Sequelize get N:M association from foreign key

我有一个架构,其中我的资产有很多威胁,每个威胁都有很多对策。我想要做的是给定资产的 id 以获取该资产的威胁列表,以及针对每个威胁的对策列表。我怎样才能做到这一点?

我认为我的代码不会提供任何帮助,但无论如何,请继续:

Asset.belongsToMany(Threat, { through: 'asset-threat' });
Threat.belongsToMany(Asset, { through: 'asset-threat' });

Countermeasure.belongsToMany(Threat, { through: 'countermeasure-threat' });
Threat.belongsToMany(Countermeasure, { through: 'countermeasure-threat' });

所有协会的东西都在这里。我让 sequelize 处理所有与外键和非外键有关的事情。

编辑

我尝试做的事情没有用,但很好地解释了我想要实现的目标如下:

const asset = await Asset.findOne({ where: { id } });

return await asset.getThreats({
    include: Countermeasure
});

至少您应该将 include 选项值表示为数组:

return await asset.getThreats({
    include: [Countermeasure]
});