首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当模型之间的关系已经定义时,SequelizeEagerLoadingError

当模型之间的关系已经定义时,SequelizeEagerLoadingError
EN

Stack Overflow用户
提问于 2017-12-04 07:01:14
回答 1查看 11.7K关注 0票数 4

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

代码语言:javascript
运行
复制
// 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'
        });
}

但是当我试图包括这样的模型时:

代码语言:javascript
运行
复制
 _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!,原因是什么?我已经初始化了两个模型之间的关系。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-05 12:13:37

我解决了这个问题。关系被定义为belongsTo,必须更改为hasOne,因为在findAll查询中应用的join类型。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47628099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档