Project.belongsToMany(User, {through: 'UserProject'});
User.belongsToMany(Project, {through: 'UserProject'});
我想让项目的用户名为'john'
include: [
{
model: model.User,
where: { name: 'john' },
},
],
我想要结果数据,
[
name: 'proejectA',
Users: [
{ name: 'john' }, { name: 'mike' }, { name: 'dude' } ...
]
]
但这给了我
[
name: 'proejectA',
Users: [
{ name: 'john' }
]
]
我希望“全用户”项目的用户名为“ john”。
可以查询吗?
答案 0 :(得分:2)
您不能简单地做到这一点,但是有一种方法可以实现:
为此,您必须与alias
定义一个关联,并再定义一个include
:
Project.belongsToMany(User, {through: 'UserProject'});
User.belongsToMany(Project, {through: 'UserProject'});
User.belongsToMany(Project, {through: 'UserProject',as: 'projectUsers'}); // <--- HERE
include: [
{
model: model.User,
where: {
name: 'john'
}
},
{
model: model.User,
as : 'projectUsers'
}
],
这将产生如下的结果:
[
name: 'proejectA',
projectUsers: [
{ name: 'john' }, { name: 'mike' }, { name: 'dude' } ...
],
Users: [
{ name: 'john' }
]
]