0
沿着接合的锯齿ASSOCATION我有Posts
和Users
基本应用程序。我想,以确保用户只提出对符合下列条件的职位:Sequelize.js与非混叠关联
- 他们以前没有
- 看到这个帖子,他们是不是这个帖子的作者
要为此,我有这些协会:
User.hasMany(Post, {
foreignKey: 'user_id'
});
User.belongsToMany(Post, {
as: {
singular: 'ViewedPost',
plural: 'ViewedPosts'
},
through: UserPostView,
foreignKey: 'user_id',
otherKey: 'post_id'
});
Post.belongsToMany(User, {
as: {
singular: 'ViewUser',
plural: 'ViewUsers'
},
through: UserPostView,
foreignKey: 'post_id',
otherKey: 'user_id'
});
我能满足每个条件有一个单独的查询,但是,我希望能够重新使用单个查询转换符合我的条件的帖子。
这里是我现在使用的获取用户以前没有见过的职位查询。我怎样才能修改这个查询来返回未由用户创作的帖子?
function fetchFreshPost(user) {
return user.getViewedPosts({
attributes: ['id'],
joinTableAttributes: []
}).then(function(posts) {
var postIds = _.map(posts, 'id');
var query = {
limit: 1,
where: {
id: {
$notIn: postIds
}
}
};
return Post.findAll(query);
})
}
谢谢你的时间。
感谢@Keval - 不幸的是,这仍然需要两个查询,一个'getViewedPosts'来获取用户的浏览帖子,然后第二个与地方条款。我试图把这个结合成一个查询。 – lightyrs