sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

beforeSave not called before upsert (^6.3.0)

Open clement-faure opened this issue 5 years ago • 2 comments

Issue Description

When upserting a document, hooks applied on model are not trigerred.

What are you doing?

// Model
Model.beforeSave((instance, options) => {
  console.log("before save hooks called");
  instance.test = "hello";
});

// Server
Model.upsert({ ... })

What do you expect to happen?

Trigerring beforeSave when upserting

Environment

  • Sequelize version: "^6.3.0"
  • Node.js version: "v12.16.1"
  • Operating System: macOS Cataline - v10.15.5

How does this problem relate to dialects?

  • [X ] I think this problem happens regardless of the dialect.
  • [ ] I think this problem happens only for the following dialect(s):
  • [ ] I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • [ ] Yes, I have the time and I know how to start.
  • [ ] Yes, I have the time but I don't know how to start, I would need guidance.
  • [ ] No, I don't have the time, although I believe I could do it if I had the time...
  • [X ] No, I don't have the time and I wouldn't even know how to start.

clement-faure avatar Jul 06 '20 09:07 clement-faure

This issue has been raised before https://github.com/sequelize/sequelize/issues/3965#issuecomment-244293255, I am tagging this as a feature request

sushantdhiman avatar Jul 07 '20 04:07 sushantdhiman

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

github-actions[bot] avatar Nov 02 '21 08:11 github-actions[bot]

I understand the reasoning in https://github.com/sequelize/sequelize/issues/3965#issuecomment-113399380 for create and update hooks not fired for upsert, but for save hook this should be no brainer, right? Sequelize can fire beforeSave/afterSave for upsert because the data are going to be saved in both cases. Is there any other issue I don't see? Can you make this a higher priority, please?

miso-belica avatar Apr 06 '23 08:04 miso-belica

Not really, beforeSave is called when save is called, and upsert does not use save, so there is nothing to fix here after all.

ephys avatar Jun 22 '23 06:06 ephys

As far as I know beforeSave is called also for Model.create for example because it saves the instance. The same way as Model.upsert saves it. It is about semantics, not about matching the method name.

miso-belica avatar Jun 22 '23 07:06 miso-belica

It is about matching the method name because the hook lets your modify the option it gives you. If we made upsert arbitrarily call beforeSave with its option, you'd get two completely different option types in beforeSave. That's not something we can do. Instead I recommend hooking both beforeSave and beforeUpsert

ephys avatar Jun 22 '23 07:06 ephys