Sequelize onDelete not working

会有一股神秘感。 提交于 2019-11-28 01:50:20

问题


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


回答1:


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

Contact.belongsTo(models.Users, {
    foreignKeyConstraint: true
    , onDelete: 'cascade'
})



回答2:


Update association to User.hasMany(models.Contact, { onDelete: 'cascade', hooks: true }) to allow the hooks between associations to fire in order to enable the onDelete cascade. Adapted from the sequelize docs:

However, adding hooks: true explicitly tells Sequelize that optimization is not of your concern and will perform a SELECT on the associated objects and destroy each instance one by one in order to be able to call the hooks with the right parameters.



来源:https://stackoverflow.com/questions/23634139/sequelize-ondelete-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!