我有一个包含所有后缀模型的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!,原因是什么?我已经初始化了两个模型之间的关系。
发布于 2017-12-05 12:13:37
我解决了这个问题。关系被定义为belongsTo,必须更改为hasOne,因为在findAll查询中应用的join类型。
https://stackoverflow.com/questions/47628099
复制相似问题