【发布时间】:2020-03-30 18:33:36
【问题描述】:
所以我有下一个结构:评论、用户、喜欢。
用户点赞多,评论点赞多。
我正在尝试获取所有评论并检查用户是否喜欢它们。我有一个使用 LEFT JOIN 的原始查询,它工作得很好:
await sequelize.query(`SELECT comments.*, likes.liked FROM comments LEFT JOIN
likes ON likes.commentId = comment.id AND likes.user_id = '123'`, {type: Sequelize.QueryTypes.SELECT});
我得到了这样的东西:
[{
"id": 1,
"userId": "123",
"comment": "abcde",
"liked": true
},
{
"id": 2,
"userId": "552",
"comment": "abc",
"liked": null
}]
现在我正在尝试使用findAll() 方法实现相同的功能。
await Comment.findAll({
include: [{
model: Like,
attributes: ['liked'],
where: {user_id: id},
required: false
}]
})
但我得到了这个:
[{
"id": 1,
"userId": "123",
"comment": "abcde",
"likes": [{liked:true}]
},
{
"id": 2,
"userId": "552",
"comment": "abc",
"likes": []
}]
所以问题是:我怎样才能只包含列liked 而不是likes 的数组?谢谢。
【问题讨论】:
标签: mysql node.js sequelize.js