我有一个模型,其中'匹配'有很多'MatchRoundTypes'
匹配具有uuid的主键 matchRoundTypes引用匹配matchesUUID作为列名。
当我尝试在包含MatchRoundTypes的Matches上进行查找时,我得到:
DEBUG:错误:MatchRoundTypes与Matches没有关联!
我的查询如下:
Matches.findAll({where: ["isPublished = ?", true], include: [MatchRoundTypes]})
我在所有这些之前发出了以下命令:
Matches.hasMany(MatchRoundTypes, { as: 'roundMaps', foreignKey: 'matchUUID', useJunctionTable: false})
我在hasMany语句中尝试了很多变体...包括foreignKey而不是等等。
这是我的Matches模型:
sequelize.define('Matches', {
uuid: {type: Seq.STRING(36), allowNull: false, primaryKey: true},
name: {type: Seq.STRING(64), allowNull: false},
}, {tableName: 'match', freezeTableName: true})
这是我的MatchRoundTypes模型:
sequelize.define('MatchRoundTypes', {
{
uuid: {type: Seq.STRING(36), allowNull: false, primaryKey: true},
roundTypeUUID: {type: Seq.STRING(36), allowNull: false},
roundName: {type: Seq.STRING(64), allowNull: true},
matchUUID: {
type: Seq.STRING(36),
references: "match",
referencesKey: "uuid",
allowNull: false
}
}, {tableName: 'matchRoundTypes', freezeTableName: true})
欢迎任何见解。
答案 0 :(得分:1)
在定义hasMany关联时使用别名,因此在执行预先加载时你还必须告诉sequelize使用该别名:
Matches.findAll({
where: ["isPublished = ?", true],
include: [{ model: MatchRoundTypes, as: 'roundMaps' }]
})