繁体   English   中英

获取嵌套的BelongsToMany Association

[英]Fetching Nested BelongsToMany Association

所以我有一些Projects ,它们在Roles中有Investigators ,并且在BudgetPeriod Effort作为一个Role (听起来很愚蠢,不是这样)。 我不知道哪些关键字实际上适合甚至搜索这个。 所以这里:

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});

在我的查询中,我得到一个项目并获取其相关的BudgetPeriods和Investigators / Roles。 我不知道如何获得Effort,因为它与关系模型相关联Roles not Investigator和Role并没有根据sequelize直接与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'
      }
    }
  ]
}

我不知道如何在此查询中获得Effort。 我尝试将{model: Investigator, as: 'investigators'},更改为{model: Investigator, as: 'investigators', include: [{model: Effort, as: 'effort'}]},以及其他变体。 我还尝试将{model: BudgetPeriod, as: 'effort'}和其他变体放在root include ,但是我这些都导致像BudgetPeriod (effort) is not associated to Investigator!这样的消息与BudgetPeriod (effort) is not associated to Investigator!

之所以如此奇怪,是因为我必须通过另一个关系模型将模型与关系模型联系起来。

感谢您提供的任何帮助!

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

数据库定义:无变化

数据库关系:

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 ... */
      ]
    }
  ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM