【问题标题】:Nodejs Sequelize Associations/Relations includeNodejs Sequelize 关联/关系包括
【发布时间】:2016-03-16 17:32:43
【问题描述】:

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

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 方法,它可以工作,但这不是我想要的。

【问题讨论】:

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


    【解决方案1】:

    您需要从视频中的频道指定属于依赖项才能在视频模型查询中使用“包含”。

    video.belongsTo(channel);
    

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

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

    【讨论】:

      猜你喜欢
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 2022-06-12
      • 2018-05-15
      相关资源
      最近更新 更多