Sequelize ORM,Hooks不与协会合作

时间:2018-05-13 13:49:25

标签: associations sequelize.js hook

我有2个模型,一个部门和一个工作人员,他们通过belongsToMany协会关联。

现在我需要在调用staff.addDepartment时调用函数。 我试图使用钩子和sequelize文档说明"当使用添加/设置功能时,beforeUpdate / afterUpdate挂钩将运行。"

enter image description here

但是经过一段时间的努力,试图让它成功之后,在更新后#39;调用addDepartment时仍未调用

员工模式

module.exports = (sequelize, DataTypes) => {
      const Staff = sequelize.define('Staff', {          
        workingLocation: {
          type: DataTypes.STRING,
          allowNull: true,
        },           
      },
      {
        freezeTableName: true,
        hooks: {
          afterCreate: (instance, options) => {
            console.log('\n\n\nSTAFF -> Hook associting staff to department: \n\n\n', staffDepartment);
          },            
          beforeUpdate: (instance, options) => {
            console.log('\n\n\n Before update model \n\n\n', staffDepartment);
          },
          afterUpdate: (instance, options) => {
            console.log('\n\n\nAfter update model \n\n\n', staffDepartment);
          },
        },
      }
      );

        Staff.associate = (models) => {

          Staff.belongsTo(
            models.Person,
            {
              foreignKey: {
                allowNull: false,
              },
              onDelete: 'CASCADE',
            }
          );


          Staff.belongsToMany(models.Department,
            {
              through: 'StaffDepartment',
              onDelete: 'CASCADE',
              onUpdate: 'CASCADE',                 
            }
          );
        };

      return Staff;
    };

部门模型

module.exports = (sequelize, DataTypes) => {
  const Department = sequelize.define('Department', {
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
    },
    acronym: {
      type: DataTypes.STRING,
      allowNull: true,
    },
  },
  {
    freezeTableName: true,
    hooks: {
      afterCreate: (staffDepartment, options) => {
        console.log('\n\n\Department -> Hook associting staff to department: \n\n\n', staffDepartment);
      },         
      beforeUpdate: (staffDepartment, options) => {
        console.log('\n\n\n Department Before update model \n\n\n', staffDepartment);
      },
      afterUpdate: (staffDepartment, options) => {
        console.log('\n\n\n Department After update model \n\n\n', staffDepartment);
      },
    },
  }
  );

  Department.associate = (models) => {         
    Department.belongsToMany(models.Staff,
        {
          through: 'StaffDepartment',
          onDelete: 'CASCADE',
          onUpdate: 'CASCADE',             
        }
      );

    Department.hasMany(models.Course,
      {
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE',
      }
    );
  };

  return Department;
};

我想要的代码在Staff Model上触发了afterUpdate

  Staff.findById(staff.id).then((s) => {                  
                s.addDepartment(departmentId);                
            });

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但是找到了解决方案。在表之间建立关联时,如果要在更新记录之前运行钩子,则必须使用afterBulkUpdate

答案 1 :(得分:0)

对于任何面临此问题的人来说,对我有用的是配置 belongsToMany 与模型的关联。

在您的情况下,它将是 { through: StaffDepartment, hooks: true } 而不是在 { through: 'StaffDepartment', hooks: true } 中使用简单的字符串文字对其进行配置

在你的 StaffDepartment.js 中:

hooks: {
  afterBulkCreate: (instance, options) => {
          
  }
}

作为旁注,在我的用例中有效的是 afterBulkCreateafterBulkDestroy,因为在这里我们与关联模型而不是模型本身进行交互。