Nodejs Sequelize Associations / Relations包括

时间:2016-03-16 17:32:43

标签: node.js include associations sequelize.js findall

我试图理解为什么会出现这个问题。 例如:

var channel = sequelize.define('channel', {...})
var video = sequelize.define('video',{...})

channel.hasMany(video)

到目前为止一切顺利,但是当我尝试这个时:

video.findAll({ include: [model:channel] }).then( function(video) {
   // return video with channel, not just the number id
}

我收到了错误:

未处理拒绝错误:频道未与视频相关联!

我做错了什么?

ps:如果我为通道尝试findAll方法,包括视频模型,它可以工作,但这不是我想要的。

1 个答案:

答案 0 :(得分:0)

您需要在视频中指定来自频道的属于依赖关系才能使用" include"在视频模型查询。

video.belongsTo(channel);

在执行findAll查询之前应该解决问题。

video.findAll({ include: channel }).then( function(videos) {
   // return video with channel, not just the number id
}
相关问题