Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize onDelete not working

I have associations between two models defined as below:

For Contact Model (in a separate file)

classMethods: {
      associate: function (models){
         Contact.belongsTo(models.User)
      }
}

For User(in a separate file)

classMethods: {
     associate: function (models){
        User.hasMany(models.Contact, {onDelete: 'CASCADE'})
     }
}

The problem is when deleting the user the contact related to the user is not deleting any advice on what am I doing wrong would be helpful?

Thanks in advance

like image 589
user2019726 Avatar asked May 13 '14 14:05

user2019726


1 Answers

I think you need to define it the other way around.

Contact.belongsTo(models.Users, {
    foreignKeyConstraint: true
    , onDelete: 'cascade'
})
like image 137
Richard Ayotte Avatar answered Nov 02 '22 15:11

Richard Ayotte