函數(shù)式編程
2022-12-08 15:31:09
我想知道,如何限制我的 belongsToMany 關(guān)系。我嘗試添加限制,但出現(xiàn)此錯(cuò)誤:"message": "只有 HasMany 關(guān)聯(lián)支持 include.separate",我有 2 個(gè)表:| peoples (id, code) | people-friends (fk_user_id, fk_friend_id) // fk_friend_id is an id from user我的請(qǐng)求 : await db.People.findAll({ where: { id: parent.dataValues.id, }, include: [ { model: db.People, as: "Friends", limit: 2, // <--- LIMIT }, ],})人物模型:People.associate = (models) => { // People relations models.People.belongsToMany(models.People, { as: "Friends", through: models.PeopleFriend, foreignKey: "fk_user_id", otherKey: "fk_friend_id", })}
1 回答

哈士奇WWW
TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果您希望某個(gè)用戶的朋友限制為 2 個(gè)朋友(哪些?您必須添加一個(gè)order選項(xiàng)),您可以查詢 PeopleFriend 包括 People 模型,如下所示:
await db.PeopleFriend.findAll({
where: {
fk_user_id: parent.dataValues.id
},
limit: 2, // <--- LIMIT
order: [['Friend', 'name', 'asc']],
include: [
{
model: db.People,
as: "Friend",
},
{
model: db.People,
as: "User",
},
],
})
不要忘記將來(lái)自 PeopleFriend 的關(guān)聯(lián)添加到兩個(gè) People 鏈接。
添加回答
舉報(bào)
0/150
提交
取消