Node.js 船帆/水线填充在连接表中不起作用?
当我尝试用另一个连接表(模型)填充一个连接表(模型)时,Waterline populate为我提供了一个空集合。我使用多对多关系,如下所示: 我正在为蒙古ABS使用sails mongo适配器 示例代码 模型用户Node.js 船帆/水线填充在连接表中不起作用?,node.js,sails.js,waterline,mlab,sails-mongo,Node.js,Sails.js,Waterline,Mlab,Sails Mongo,当我尝试用另一个连接表(模型)填充一个连接表(模型)时,Waterline populate为我提供了一个空集合。我使用多对多关系,如下所示: 我正在为蒙古ABS使用sails mongo适配器 示例代码 模型用户 /user.js module.exports = { schema: true, attributes: { id: { type: 'string', primaryKey: true, }, student: {
/user.js
module.exports = {
schema: true,
attributes: {
id: {
type: 'string',
primaryKey: true,
},
student: {
type: 'string'
},
vendor: {
type: 'string'
},
courses: {
collection: 'vendorcourse',
via: 'users',
through: 'usercourse'
},
coursesProvided: {
collection: 'course',
via: 'vendors',
through: 'vendorcourse'
}
}
};
示范课程
/course.js
module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
autoIncrement: true
},
name: {
type: 'string',
required: true
},
description: {
type: 'string'
},
type: {
type: 'string'
},
vendors: {
collection: 'User',
via: 'courseProvided',
through: 'vendorcourse'
}
}
};
module.exports = {
tableName: 'vendorcourse',
tables: ['user', 'course'],
junctionTable: true,
attributes: {
id: {
type: 'string',
autoIncrement: true,
primaryKey: true
},
name: {
type: 'string',
required: true
},
course: {
columnName: 'course',
type: 'string',
foreignKey: true,
references: 'course',
on: 'id',
via: 'vendors',
groupBy: 'course',
required: true
},
vendor: {
columnName: 'vendor',
type: 'string',
foreignKey: true,
references: 'user',
on: 'id',
via: 'coursesProvided',
groupBy: 'user',
required: true
},
users: {
collection: 'user',
via: 'courses',
through: 'usercourse'
}
}
};
/usercourse.js
module.exports = {
tableName: 'usercourse',
tables: ['user', 'vendorcourse'],
junctionTable: true,
attributes: {
id: {
type: 'string',
autoIncrement: true,
primaryKey: true
},
course: {
columnName: 'course',
type: 'string',
foreignKey: true,
references: 'vendorcourse',
on: 'id',
via: 'users',
groupBy: 'vendorcourse',
required: true
},
user: {
columnName: 'user',
type: 'string',
foreignKey: true,
references: 'user',
on: 'id',
via: 'courses',
groupBy: 'user',
required: true
}
}
};
模型供应商课程
/course.js
module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
autoIncrement: true
},
name: {
type: 'string',
required: true
},
description: {
type: 'string'
},
type: {
type: 'string'
},
vendors: {
collection: 'User',
via: 'courseProvided',
through: 'vendorcourse'
}
}
};
module.exports = {
tableName: 'vendorcourse',
tables: ['user', 'course'],
junctionTable: true,
attributes: {
id: {
type: 'string',
autoIncrement: true,
primaryKey: true
},
name: {
type: 'string',
required: true
},
course: {
columnName: 'course',
type: 'string',
foreignKey: true,
references: 'course',
on: 'id',
via: 'vendors',
groupBy: 'course',
required: true
},
vendor: {
columnName: 'vendor',
type: 'string',
foreignKey: true,
references: 'user',
on: 'id',
via: 'coursesProvided',
groupBy: 'user',
required: true
},
users: {
collection: 'user',
via: 'courses',
through: 'usercourse'
}
}
};
/usercourse.js
module.exports = {
tableName: 'usercourse',
tables: ['user', 'vendorcourse'],
junctionTable: true,
attributes: {
id: {
type: 'string',
autoIncrement: true,
primaryKey: true
},
course: {
columnName: 'course',
type: 'string',
foreignKey: true,
references: 'vendorcourse',
on: 'id',
via: 'users',
groupBy: 'vendorcourse',
required: true
},
user: {
columnName: 'user',
type: 'string',
foreignKey: true,
references: 'user',
on: 'id',
via: 'courses',
groupBy: 'user',
required: true
}
}
};
模型用户课程
/course.js
module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
autoIncrement: true
},
name: {
type: 'string',
required: true
},
description: {
type: 'string'
},
type: {
type: 'string'
},
vendors: {
collection: 'User',
via: 'courseProvided',
through: 'vendorcourse'
}
}
};
module.exports = {
tableName: 'vendorcourse',
tables: ['user', 'course'],
junctionTable: true,
attributes: {
id: {
type: 'string',
autoIncrement: true,
primaryKey: true
},
name: {
type: 'string',
required: true
},
course: {
columnName: 'course',
type: 'string',
foreignKey: true,
references: 'course',
on: 'id',
via: 'vendors',
groupBy: 'course',
required: true
},
vendor: {
columnName: 'vendor',
type: 'string',
foreignKey: true,
references: 'user',
on: 'id',
via: 'coursesProvided',
groupBy: 'user',
required: true
},
users: {
collection: 'user',
via: 'courses',
through: 'usercourse'
}
}
};
/usercourse.js
module.exports = {
tableName: 'usercourse',
tables: ['user', 'vendorcourse'],
junctionTable: true,
attributes: {
id: {
type: 'string',
autoIncrement: true,
primaryKey: true
},
course: {
columnName: 'course',
type: 'string',
foreignKey: true,
references: 'vendorcourse',
on: 'id',
via: 'users',
groupBy: 'vendorcourse',
required: true
},
user: {
columnName: 'user',
type: 'string',
foreignKey: true,
references: 'user',
on: 'id',
via: 'courses',
groupBy: 'user',
required: true
}
}
};
当我这样做的时候
usercourse.find().populate('course').exec(function(error,result){
console.log(result);
});
输出是
[{
'id': //some ID,
'course': [] // this is should be populated with the relative record from the vendorcourse model
'user' : //some ID
}]
课程字段应该包含来自vendorcourse模型的记录,但我得到的是一个空集合[]
当我这样做时,同样的命令也会起作用
usercourse.find().populate('user').exec(function(error,result){
console.log(result);
});
输出与预期一致
[{
'id': //some ID,
'course': //some ID,
'user': [{ //relevent data }]
}]
我做错了吗?或者是因为sails/waterline不支持从另一个连接表填充多对多关联尚未得到正式支持,请检查。即使你让它工作,也有可能在
0.11
版本中破坏行为。我使用了sails文档中提到的解决方法。创建了一个中间模型。如果我不清楚,很抱歉。我想知道的是如何填充连接到连接表。通过使用上述解决方法,我能够通过关系实现多对多。