2017-05-30 70 views
0

我有兩個使用Sequelize cli創建的模型;第一個型號是user_number,第二個型號是emailSign破壞級聯在Sequelize orm中不起作用

這是我user_number型號:

'use strict'; 
    module.exports = function(sequelize, DataTypes) { 
    var user_number = sequelize.define('user_number', { 
    user_id: DataTypes.INTEGER, 
    callRoute_id: DataTypes.INTEGER, 
    password: DataTypes.STRING, 
    }, { 
    underscored: true, 
    classMethods: { 
     associate: function(models) { 
      // associations can be defined here 
      user_number.hasMany(models.emailSign, { 
       foreignKey: 'user_number_id', 
       onDelete: "CASCADE", 
      }); 
      } 
      } 
     }); 
     return user_number; 
    }; 

這是我emailSign型號:

 'use strict'; 
     module.exports = function(sequelize, DataTypes) { 
     var emailSign = sequelize.define('emailSign', { 
     email: DataTypes.STRING, 
     user_number_id: DataTypes.INTEGER 
     }, { 
     underscored: true, 
     classMethods: { 
      associate: function(models) { 
      // associations can be defined here 
      emailSign.belongsTo(models.user_number, { 
      foreignKey: 'user_number_id', 

      }) 
     } 
     } 
     }); 
     return emailSign; 
      }; 

我想摧毀user_number級聯刪除emailSign。我對破壞代碼是

User_number.destroy({where:{id : 5}}).then(function(){ 

resp.status(200); 
) 

user_number ID爲5被刪除,但emailSign不會被刪除。

我該如何刪除相關的emailSign

我Sequelize版本是3.30.4

+0

https://stackoverflow.com/questions/23634139/sequelize-ondelete-not-working – FiftiN

+0

坦但它不起作用 – majid

回答

0

你應該定義上屬於關聯關聯onDelete行動

emailSign.belongsTo(models.user_number, { 
    foreignKey: 'user_number_id', 
    onDelete: 'CASCADE' 
});