【问题标题】:Sequelize, one-to-many association, find with includeSequelize, 一对多关联, find with include
【发布时间】:2014-12-27 04:19:44
【问题描述】:

我无法理解为什么 find 在尝试急切加载关联时会抛出错误。

相关型号:

标签:

module.exports = function(sequelize, DataTypes) {
  var Tag = sequelize.define('Tag',
  {
    tag_name: {type: DataTypes.STRING(30), allowNull: false}
  },  
  {
    associate: function(models) {
      Tag.hasMany(models.Event, {foreignKeyConstraint: true});
      Tag.hasMany(models.Retailer, {foreignKeyConstraint: true});
  }   
}); 
  return Tag;
};

事件标签:

module.exports = function(sequelize, DataTypes) {
  var EventTag = sequelize.define('EventTag',
  {
    cor_name: DataTypes.STRING
  },
  {
    associate: function(models) {
      EventTag.belongsTo(models.Tag, {foreignKeyConstraint: true});
      EventTag.belongsTo(models.Event, {foreignKeyConstraint: true});

    }
  });
  return EventTag;
};

当我进行查找时,我收到一条错误消息“EventTag not associated to Tag!” 这是发现:

db.Tag.find({
  where: ['id = ' + req.param('activity')],
  include: [
    {model: db.EventTag,
     include: [
       {model: db.Event,
        where: [where]}]
    }], 
  attributes: [
    'id',
    'tag_name']})

我在这里缺少什么?我什至尝试了 findAll ,但出现了同样的错误。 我正在使用 sequelize 1.7.10,如果我升级到最新版本会有帮助吗?

【问题讨论】:

    标签: node.js sequelize.js model-associations


    【解决方案1】:

    我发现了问题。 我忽略了:

    Tag.hasMany(models.Event, {foreignKeyConstraint: true});
    

    应该是:

    Tag.hasMany(models.EventTag, {foreignKeyConstraint: true});
    

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 2016-02-05
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 2019-06-23
      • 1970-01-01
      相关资源
      最近更新 更多