我有两个相互关联的模型如下:
TeamFormationMinutes.associate = (models) => {
models.TeamFormationMinutes.belongsTo(models.Game);
}
Game.associate = (models) => {
models.Game.hasMany(models.TeamFormationMinutes);
};
我想创建一个可以模拟这个原始查询的查询:
select distinct(T.game_id) from TeamFormationMinutes T inner join Games G on T.game_id = G.id where G.competition_id=112 and G.season=2016;
这就是我所做的:
const leagueSeasonMatches = await models.TeamFormationMinutes
.findAll({
include: [{
model: models.Game,
where: {competition_id: 112, season: 2016}
}],
plain: true,
distinct: 'game_id'
});
但是当我运行它时出现SequelizeEagerLoadingError: Game is not associated to TeamFormationMinutes!
错误。就我而言,我定义了这两个模型之间的关联。我在这里缺少什么?
模型如:
'use strict';
module.exports = (sequelize, DataTypes) => {
const TeamFormationMinutes = sequelize.define('TeamFormationMinutes', {
team_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
team_formation_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
game_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
duration: {
type: DataTypes.INTEGER,
primaryKey: true
}
}, {
underscored: true
});
TeamFormationMinutes.associate = (models) => {
models.TeamFormationMinutes.belongsTo(models.Game);
models.TeamFormationMinutes.belongsTo(models.Team);
models.TeamFormationMinutes.belongsTo(models.TeamFormation);
};
return TeamFormationMinutes;
};