[英]Sequelize self-referential many-to-many; how to specify what I'm joining on
I have a Sequelize structure that associates my User
table to itself through a Follow
table, with follower
and following
.我有一个 Sequelize 结构,它通过
Follow
表将我的User
表与自身关联,并带有follower
和following
。 I'm 99% certain that I have all of the necessary pieces for this to be working the way I expect it to;我有 99% 的把握,我拥有所有必要的部件,可以按照我期望的方式工作; I've the aliased
我有别名
User.belongsToMany(models.User, { through: models.Follow, as: 'Followers', foreignKey: 'follower'});
User.belongsToMany(models.User, { through: models.Follow, as: 'Following', foreignKey: 'following'});
as well as也
User.hasMany(models.Follow, { foreignKey: 'following' });
User.hasMany(models.Follow, { foreignKey: 'follower' });
and和
Follow.belongsTo(models.User, { foreignKey: 'following', as: 'Following' });
Follow.belongsTo(models.User, { foreignKey: 'follower', as: 'Follower' });
And I'm able to call我可以打电话
User.findByPk(id, {
include: {
model: Follow
}
});
which returns a single user and an array of Follow entries.它返回一个用户和一个 Follow 条目数组。
The problem I'm having is, Sequelize seems to be defaulting to creating the Follow
query as where: { followING: User.id }
rather than where: followER
, such that if I have 1 user FOLLOWING themselves and 2 other people, but only FOLLOWED BY themselves, it only returns 1 result from my Follow table.我遇到的问题是,Sequelize 似乎默认创建
Follow
查询为where: { followING: User.id }
而不是where: followER
,这样如果我有 1 个用户 FOLLOWING 自己和 2 个其他人,但只有FOLLOWED BY 他们自己,它只从我的 Follow 表中返回 1 个结果。 As I was cycling through the primary keys for the handful of users in my seed, only the following
value in my results change, such that I'm only ever returning a users' followers, not the other users that user is following.当我在我的种子中为少数用户循环访问主键时,我的结果中只有
following
值会发生变化,这样我只会返回用户的关注者,而不是用户关注的其他用户。
Is there a way to specify in an include
which specific column I'm trying to join on, when multiple columns match the Sequelize object to which I'm joining?当多个列与我要加入的 Sequelize object 匹配时,有没有办法在
include
我尝试加入的特定列中指定?
I understand that worst case scenario I can always skip the User
step and go straight to我知道最坏的情况我总是可以跳过
User
步骤和 go 直接到
Follow.findAll({
where: {
follower: id
}
})
But that restricts my immediate access to the User object and I'd have to write an additional query, which seems cumbersome considering a self-associated many-to-many capability exists.但这限制了我对用户 object 的立即访问,我必须编写一个额外的查询,考虑到存在自关联的多对多功能,这似乎很麻烦。
If you wish to get a user with followers or with following users you don't need to indicate Follow
table.如果您希望获得具有关注者或关注用户的用户,则无需指明
Follow
表。 All you need is to indicate an alias of a required association:您只需要指明所需关联的别名:
User.findByPk(id, {
include: {
model: User,
as: 'Followers'
}
});
User.findByPk(id, {
include: {
model: User,
as: 'Following'
}
});
In case you need to include Follow
directly you need to add an associations like this:如果您需要直接包含
Follow
,则需要添加如下关联:
User.hasMany(models.Follow, { as: 'FollowerLinks', foreignKey: 'follower'});
User.hasMany(models.Follow, { as: 'FollowingLinks', foreignKey: 'following'});
And you also should indicate an alias so that way Sequelize will know what association to use:您还应该指定一个别名,以便 Sequelize 知道要使用什么关联:
User.findByPk(id, {
include: {
model: Follow,
as: 'FollowerLinks'
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.