假设我有 2 个模型,用户和事件。它们被设置为 m:m 关系 User.belongsToMany(Event)
和 Event.belongsToMany(User)
。这一切都很好,但是当我执行 User.findAndCountAll({include: [{model: Event}]})
时,result.rows 的结果 json 看起来像
[
...
{
userdata...,
events: [ // included by model relation
...included event data...,
junction_table: { // I dont want this data sent back to client, just above data
...data i dont want...
}
}
...
]
我要
[
{
userdata...,
events: [
{ included event data without junction_table }
]
}
]
有没有办法去除由于某种原因自动包含在每个事件中的 junction_table 而无需对数据进行映射并重建它?我宁愿不把我不需要的所有这些数据一直通过网络发送给客户。我尝试只在模型上指定 attributes: []
,但它不会消失。
最佳答案
根据这个 SO question,这可以通过指定您不想要“直通”表属性来实现。 .
在您的情况下,它将是:
User.findAndCountAll({
include:
[{model: Event, through: { attributes: [] }}]
});
关于javascript - sequelize 不在关联模型数据中包含联结表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47586259/