Sequelize使用问题记录

 

1. 钩子(Hooks)

几个钩子函数的是有执行顺序的,一定要注意。

(1)
  beforeBulkCreate(instances, options, fn)
  beforeBulkDestroy(options, fn)
  beforeBulkUpdate(options, fn)
(2)
  beforeValidate(instance, options, fn)
(-)
  validate
(3)
  afterValidate(instance, options, fn)
  - or -
  validationFailed(instance, options, error, fn)
(4)
  beforeCreate(instance, options, fn)
  beforeDestroy(instance, options, fn)
  beforeUpdate(instance, options, fn)
  beforeSave(instance, options, fn)
  beforeUpsert(values, options, fn)
(-)
  create
  destroy
  update
(5)
  afterCreate(instance, options, fn)
  afterDestroy(instance, options, fn)
  afterUpdate(instance, options, fn)
  afterSave(instance, options, fn)
  afterUpsert(created, options, fn)
(6)
  afterBulkCreate(instances, options, fn)
  afterBulkDestroy(options, fn)
  afterBulkUpdate(options, fn)

遇到的问题:设置了beforeUpdate的钩子函数,update时总是调用不到。

原因:

// 先触发了beforeBulkUpdate钩子
MyModel.update(mModel,
    {where:{col_1 : val_1}}
);

解决:

// 强制为每一行都更新
beforeBulkUpdate: function(options){
  options : { individualHooks : true }
}
// 或者在每一次update时激活这个options
MyModel.update(mModel, { where: {col_1 : val_1}, individualHooks: true });

参考:

[1] Sequelize doc - update(values, options)

[2] Sequelize issue #4190

 

转载于:https://my.oschina.net/u/3409026/blog/884725

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值