小编set*_*ter的帖子

在Sequelize模型钩子函数中访问其他模型

我正在尝试创建一个模型挂钩,在创建主模型时自动创建关联记录.当我的模型文件结构如下时,如何在钩子函数中访问我的其他模型?

/**
 * 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;
};
Run Code Online (Sandbox Code Playgroud)

node.js sequelize.js

10
推荐指数
2
解决办法
5257
查看次数

标签 统计

node.js ×1

sequelize.js ×1