【问题标题】:SequelizeEagerLoadingError when relationship between models has already been definedSequelizeEagerLoadingError 当模型之间的关系已经定义时
【发布时间】:2018-05-17 14:35:13
【问题描述】:

我有一个exports file,其中包含所有的续集模型,然后定义模型之间的关系。它看起来像:

// Snippet from the global init file

for (let modelFile of modelFileList) {
            // ... Some code ...

            // Require the file
            appliedModels[modelName] = require(`../${MODEL_DIR}/${modelFile}`).call(null, _mysql);
 }

 //Define the relationship between the sql models
 _defineRelationship(appliedModels);


function _defineRelationship(models) {
     models._planAllocationModel.belongsTo(models._subscriptionModel, {
            foreignKey: 'subscription_id',
            targetKey: 'subscription_id'
        });
}

但是当我尝试包含以下模型时:

 _subscriptionModel.findAll({
                where: {
                    start_date: {
                        _lte: today // Get all subscriptions where start_date <= today
                    }
                },
                limit,
                include: [
                    {
                        model: _planAllocationModel
                    }
                ]
            });

sequelize 抛出错误:SequelizeEagerLoadingError: tbl_plan_allocation is not associated to tbl_subscription_info! 这可能是什么原因?我已经初始化了两个模型之间的关系。

【问题讨论】:

    标签: node.js orm sequelize.js


    【解决方案1】:

    我能够解决问题。该关系被定义为belongsTo,由于findAll 查询中应用的join 类型,必须将其更改为hasOne

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-18
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 2021-04-19
      • 2020-09-02
      相关资源
      最近更新 更多