【发布时间】:2018-04-07 01:17:01
【问题描述】:
我是 sequelize 的新手,并试图确定如何对归属关系执行查询,我需要检查子关系是否存在,但我不想带来它的任何字段。
Project.belongsToMany(models.User, {
through: 'userproject', as: 'links', foreignKey: 'project_id', otherKey: 'user_id'
});
我的尝试:
const linkedProjects = this.model.findAll({
attributes: ['Project.*'],
include: [{
model: models.User,
as: 'links',
where: {
userid: user.sub
}
}]
});
但是这仍然给我带来了预计的链接数据,除了 where 部分之外,这对我来说没用。
没有“原始查询”的最佳方法是什么? 我正在寻找,基本上:
select p.* from project p
inner join userproject up on p.project_id = up.project_id
inner join user u on up.user_id = u.id
where u.userid = 'xxx'
【问题讨论】:
-
您是否尝试在
include数组中的对象内添加attributes: []? -
那行得通。想把它作为答案,以便我可以接受吗?谢谢
-
我刚刚添加它作为答案:)
标签: sequelize.js has-and-belongs-to-many