【发布时间】:2019-10-09 20:04:32
【问题描述】:
我正在使用 sequelize,我有一个带有两个外键的模型
app.schemas.messengers.belongsTo(app.schemas.users, {
foreignKey: 'id_user_to',
as: 'to'
});
app.schemas.messengers.belongsTo(app.schemas.users, {
foreignKey: 'id_user_from',
as: 'from'
});
并且查询的结果必须返回这个特定用户的所有消息 这是查询的代码
return Users.findAll({
attributes: ['uuid', 'id', 'profile_pic', 'firstname', 'lastname', 'online'],
where: whereUser,
include: [{
model: Messengers,
as: 'from',
// as: 'to',
where: whereMessenger,
$or: [{
id_user_to: user.id,
},
{
id_user_from: user.id
}
],
order: [
['createdAt', 'ASC'],
],
}]
})
但只返回给我写信的用户的消息,而不是给我写的用户的消息。 有什么办法可以让我在 sequelize 的 as 属性上放两个别名,还是有其他方法可以这样做?
【问题讨论】:
标签: node.js sequelize.js