Skip to content

destroy() in paranoid mode does not persist properties updated in a beforeDestroy hook #9318

Closed

Description

@ddolcimascolo

I'm opening a pull request for this in a minute, but I wanted to raise the issue first.

What are you doing?

I have a model in paranoid mode. This model gets a beforeDestroy hook attached in order to update a property on deletion (updatedBy which is a FK to another model, but this is not related to the problem described here).
When calling destroy(), instances are correctly soft deleted by my changed property is not persisted.

const ParanoidUser = this.sequelize.define('ParanoidUser', {
  username: Sequelize.STRING,
  updatedBy: Sequelize.INTEGER
}, {
  paranoid: true,
  hooks: {
    beforeDestroy: instance => instance.updatedBy = 1
  }
});

return ParanoidUser.sync({ force: true })
  .then(() => ParanoidUser.create({ username: 'user1' }))
  .then(user => user.destroy())
  .then(() => ParanoidUser.findOne({ paranoid: false }))
  .then(user => console.log(user.updatedBy));

What do you expect to happen?

1

What is actually happening?

null

Dialect: mysql / sqlite
Dialect version: 5.7
Database version: 5.7
Sequelize version: 4.36.1
Tested with latest release: Yes, 5.0.0-beta2

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. added a commit that references this issue on Apr 17, 2018
    591e2ca
  2. heisian commented on Apr 19, 2018

    @heisian

    This is pretty simple - you need to call save or update.

    Your beforeDestroy hook should actually look like this:

    hooks: {
      beforeDestroy: async (instance) => {
        instance.updatedBy = 1
        await instance.save()
      }
    }

    ...or this:

    hooks: {
      beforeDestroy: async (instance) => {
        await instance.update({ updatedBy: 1 })
      }
    }

    There's no way for Sequelize to know that you want to save anything unless you tell it to in the hook. Values don't get bubbled up to the destroy function, that would cause unexpected behaviour.
    @mickhansen

  3. sushantdhiman commented on Apr 19, 2018

    @sushantdhiman
    Contributor

    Other hooks like beforeCreate / beforeUpdate can modify instance and modified value will be save, I would say beforeDestroy should allow that too when dealing with paranoid models which is internally updating records.

  4. added
    type: bug
    on Apr 19, 2018
  5. added 2 commits that reference this issue on Apr 19, 2018
    d1be757
    bb42b17
  6. heisian commented on Apr 19, 2018

    @heisian

    @sushantdhiman I feel like that could have unintended consequences unless it's really made clear in the docs what the discrepancy in behaviours between different hook types are.

  7. tomprogers commented on May 22, 2018

    @tomprogers

    @heisian The official documentation states very explicitly that you can make arbitrary changes to the model, and they will be persisted:

    Arguments to hooks are passed by reference. This means, that you can change the values, and this will be reflected in the insert / update statement.

    The example code below that text very strongly suggests that simple assignment of new values to model attributes is sufficient.

    If callers must manually invoke save/update within the hook (in some or all cases), then the documentation needs to be corrected.

  8. tomprogers commented on May 23, 2018

    @tomprogers

    @heisian I should add that performing save/update on the entity being destroyed prevents it from being destroyed.

    My hook:

    beforeDestroy: ( person, options ) => {
    	console.log(`Person.beforeDestroy hook invoked`)
    	return person.update( { name: `${person.name} (deleted @ ${new Date().toISOString()})` } )
    }

    My destroy call:

    return Person.destroy({
    	where: { name: personName },
    	individualHooks: true
    })

    Paranoid is turned on and has been working. Because I have a uniqueness constraint on names, I want to modify the names of soft-deleted items, to release the name back into the pool (so to speak). Appending the deletion timestamp is a pretty common practice, and that's what I'm attempting to do.

    When I run the destroy call, I see the log statement, I see the SQL update statement that modifies the name, but the soft-delete SQL never executes.

    When I comment out the update call, soft-deletion works normally, but the name is obviously not updated.

    I think your advice is not going to work for people.

    In my specific case, I was able to get good results by making this an afterDestroy hook instead. I hope that helps someone.

  9. danedavid commented on Jun 27, 2018

    @danedavid

    Wouldn't calling save() or update() trigger beforeUpdate() ? If so , does it happen synchronously, that is, is the instance deleted only after beforeUpdate has completed execution ?
    If not, I think @tomprogers 's case would be justified.

  10. added a commit that references this issue on Jul 9, 2018
    dc27ef9
  11. added a commit that references this issue on Oct 28, 2018
    8aff765
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions