Node.js 续写别名
我正在使用sequelize,我有一个带有两个外键的模型Node.js 续写别名,node.js,sequelize.js,Node.js,Sequelize.js,我正在使用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' }); 查询结果必须返回该特定用户
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属性上添加两个别名,或者有其他方法可以这样做?您必须包含两次,例如
...
include: [
{
model: Messengers,
as: 'from'
/* other stuff */
},
{
model: Messengers,
as: 'to'
/* other stuff */
}
],
...
此外,您的别名可能会有问题,因为“to”和“from”是保留字。我建议改为msgTo和msgFrom…在哪里保留
到
和从
到?“这是续集问题还是其他问题?”brad在SQL中保留。例如,SELECT*FROM myTable As FROM
不起作用。。。。