本文介绍了在 Sequelize 模型钩子函数中访问其他模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个模型挂钩,该挂钩在创建主模型时自动创建关联记录.当我的模型文件结构如下时,如何在钩子函数中访问我的其他模型?
I'm trying to create a model hook that automatically creates an associated record when the main model has been created. How can I access my other models within the hook function when my model file is structured as follows?
/**
* Main Model
*/
module.exports = function(sequelize, DataTypes) {
var MainModel = sequelize.define('MainModel', {
name: {
type: DataTypes.STRING,
}
}, {
classMethods: {
associate: function(models) {
MainModel.hasOne(models.OtherModel, {
onDelete: 'cascade', hooks: true
});
}
},
hooks: {
afterCreate: function(mainModel, next) {
// ------------------------------------
// How can I get to OtherModel here?
// ------------------------------------
}
}
});
return MainModel;
};
推荐答案
可以通过sequelize.models.OtherModel
访问其他模型.
You can access the other model through sequelize.models.OtherModel
.
这篇关于在 Sequelize 模型钩子函数中访问其他模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!