Sequelize onDelete not working

前端 未结 2 1678
天涯浪人
天涯浪人 2020-12-18 13:08

I have associations between two models defined as below:

For Contact Model (in a separate file)

classMethods: {
      associate: function (models){
          


        
相关标签:
2条回答
  • 2020-12-18 13:20

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

    Contact.belongsTo(models.Users, {
        foreignKeyConstraint: true
        , onDelete: 'cascade'
    })
    
    0 讨论(0)
  • 2020-12-18 13:27

    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.

    0 讨论(0)
提交回复
热议问题