我有以下型号:
Users
haveMany Posts
Posts
haveMany Comments
使用 Sequelize 有没有办法使用关系获取给定 Comments
的所有 User
?而不是自定义查询?
我正在寻找:
Users
hasMany Comments
到 models.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 年历史了,我希望有些东西可能已经改变了。