我有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!
这可能是什么原因?我已经初步确定了两个模型之间的关系。
答案 0 :(得分:1)
我能够解决问题。该关系定义为belongsTo
,由于hasOne
查询中应用的join
类型,该关系必须更改为findAll
。