【发布时间】:2018-02-19 00:44:32
【问题描述】:
这段代码运行良好。我现在可以获得关注和关注者,我需要查看我获得的关注者是否也在关注他们?
这是我如何对关注者表进行另一个查询/子查询并看到我也在关注我的关注者的问题。
关注者表
export default function(sequelize, DataTypes) {
return sequelize.define('Follower', {
_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
userId: {
type: DataTypes.INTEGER,
allowNull: false
},
followingId: {
type: DataTypes.INTEGER,
allowNull: false
}
});
}
协会
db.Follower.belongsTo(db.User, {as:'following', foreignKey: 'followingId'});
db.Follower.belongsTo(db.User, {as:'follower', foreignKey: 'userId'});
查询
Follower.findAll({
where: {
followingId: userId
},
attributes: ['_id'],
include: [
{
model: User,
attributes: ['fullName', 'username', '_id', 'picture'],
as: 'follower'
}
]
})
更新
我已经实现了表单行查询的预期结果:
SELECT F.userId, F.`followingId` , F1.`followingId` as IsFollowing , U.`fullName` FROM Followers as F
INNER JOIN Users as U ON userId = U._id
LEFT JOIN Followers as F1 On F.userId = F1.followingId
WHERE F.followingId = 142
还在苦苦挣扎。
【问题讨论】:
标签: mysql node.js sequelize.js