获取嵌套的BelongsToMany Association

时间:2016-10-07 20:48:01

标签: javascript orm sequelize.js model-associations

所以Projects Investigators Roles Effort Role BudgetPeriod作为const Project = seq.define('Project', {/*...*/}); const Investigator = seq.define('Investigator', {/*...*/}); const BudgetPeriod = seq.define('BudgetPeriod', {/*...*/}); const Role = seq.define('Role', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, role: { type: DataTypes.ENUM, /*...*/ }, /*... Other role related info...*/ }); const Effort = seq.define('Effort', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, budgetedAY: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 }, /*... other effort related info...*/ }); // Each Project can have many budget periods, BP knows its Project Project.hasMany(BudgetPeriod, {as: 'budgetPeriods'}); // Each Project has many Investigators, which have a Role on Many projects Project.belongsToMany(Investigator, {as: 'investigators', through: db.Role}); // Each Investigator, in their role, commits Effort toward the Projects during a BudgetPeriod Role.belongsToMany(BudgetPeriod, {as: 'effort', through: db.Effort}); {听起来很愚蠢#39; t)。我不知道哪些关键字实际上适合甚至搜索这个。所以这里:

Roles

好的,现在关系:

Role

在我的查询中,我正在获取一个项目并获取其相关的BudgetPeriods和Investigators / Roles。 我不知道如何获取Effort,因为它与关系模型Project无关,而不是调查员,Project.findById(projectId, { include: [ {model: Investigator, as: 'investigators'}, {model: BudgetPeriod, as: 'budgetPeriods'}, ], order: [ [{model: BudgetPeriod, as: 'budgetPeriods'}, 'period', 'ASC'] ] }).then(res => console.log(res)) // Produces something like { budgetPeriods: [/*...*/], investigators: [ { id:1, name:'abc', Role: { id: 1, role: 'PI' } } ] } 根据续集不与{model: Investigator, as: 'investigators'},直接关联。< /强>

{model: Investigator, as: 'investigators', include: [{model: Effort, as: 'effort'}]},

我不知道如何在此查询中获得Effort。我尝试将{model: BudgetPeriod, as: 'effort'}更改为include以及其他变体。我还尝试将BudgetPeriod (effort) is not associated to Investigator!和其他变体放在根SimpleClientHttpRequestFactory.prepareConnection(HttpURLConnection connection, String httpMethod) 上,但我这些都会产生public class SimpleClientHttpRequestWithGetBodyFactory extends SimpleClientHttpRequestFactory { @Override protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { super.prepareConnection(connection, httpMethod); if ("GET".equals(httpMethod)) { connection.setDoOutput(true); } }

等消息

这个奇怪的原因是因为我必须通过另一个关系模型将模型与关系模型联系起来。

感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:0)

由于我不认为会有答案,我重构了数据库关系。对于那些有类似问题的人来说,这是我的改变。

数据库定义:无变化

数据库关系:

Project.hasMany(BudgetPeriod, {as: 'budgetPeriods'});
Project.belongsToMany(Investigator, {as: 'investigators', through: db.Role});
Investigator.belongsToMany(BudgetPeriod, {as: 'effort', through: db.Effort});

由于每个调查员在每个项目中可能只有一个角色,因此可以安全地假设调查员在BudgetPeriod中所做的每项工作都是由该角色完成的。

这会将我的查询更改为

Project.findById(projectId, {
  include: [
    {model: Investigator, as: 'investigators',
      include: [{model: BudgetPeriod, as: 'effort', where: {ProjectId: projectId}}]},
    {model: BudgetPeriod, as: 'budgetPeriods'},
  ],
  order:   [
    [{model: BudgetPeriod, as: 'budgetPeriods'}, 'period', 'ASC']
  ]
}).then(res => console.log(res))

// Produces something like
{
  budgetPeriods: [/*...*/],
  investigators: [
    {
      id:1, 
      name:'abc', 
      Role: {
        id: 1,
        role: 'PI'
      },
      Effort: [
        /* ... Effort ... */
      ]
    }
  ]
}