我使用 Sequelize 作为 MySQL、Node 和 Express 的 ORM。
将新项目插入“ExpertiseField”表时,我想更新不同表中的字段。
我在 Sequelize 中遇到钩子(Hook)问题,由于某种原因 afterCreate 似乎没有执行任何操作。
我的“ExpertiseField”模型:
module.exports = (sequelize, DataTypes) => {
const ExpertiseField = sequelize.define('ExpertiseField', {
name: DataTypes.STRING,
type: DataTypes.INTEGER,
position: DataTypes.INTEGER
}, {
classMethods: {
associate: function(models) {
ExpertiseField.hasOne(models.LocalDataLastUpdatedAtItem);
}
},
hooks: {
afterCreate: function(expertiseField, options) {
sequelize.models.LocalDataLastUpdatedAtItem.update({
updatedAt: expertiseField.updatedAt
},{
where: {
name: 'expertise_fields_last_updated_at'
}
});
}
}
});
ExpertiseField.associate = function(models) {
// associations can be defined here
};
return ExpertiseField;
};
我正在创建的钩子(Hook)没有做任何事情,知道为什么吗?
最佳答案
回答我的问题也许可以帮助别人避免将来犯我的错误。
显然 Sequelize Hook 仅在代码内部起作用。 当从第 3 方 SQL-Viewer 程序编辑我的表时,没有任何反应,我认为出了什么问题..但是当运行我的代码时,一切正常。
关于node.js - Sequelize afterCreate 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59601444/