Так что у меня Projects
, которые имеют Investigators
в Roles
и совершить Effort
Role
как в BudgetPeriod
(звучит глупо сложным не так). Я понятия не имею, какие ключевые слова действительно подходят даже для поиска. так что здесь идет:Fetching Уплотненный BelongsToMany Ассоциация
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 и следователей/Роли. Я не знаю, как получить Усиление, потому что оно связано с реляционной моделью Roles
не следователь и Role
не связано напрямую с Project
в соответствии с sequelize.
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'}]},
и другие варианты. Я также попытался положить {model: BudgetPeriod, as: 'effort'}
и другие вариации на корне include
, но это все результат в сообщениях как BudgetPeriod (effort) is not associated to Investigator!
Причины это так странно, потому что я должен связать модель с реляционной моделью через другую реляционную модель.
Благодарим за любую помощь, которую вы могли бы предложить!