fix: Use parent transaction for findOrCreate after-commit hook (#8173)

This commit is contained in:
Hemachandar
2024-12-31 05:41:11 +05:30
committed by GitHub
parent f448be5830
commit 41832bbaf1
2 changed files with 13 additions and 1 deletions
+6 -1
View File
@@ -83,7 +83,12 @@ class Event extends IdModel<
options: SaveOptions<InferAttributes<Event>>
) {
if (options.transaction) {
options.transaction.afterCommit(() => void globalEventQueue.add(model));
// 'findOrCreate' creates a new transaction always, and the transaction from the middleware is set as its parent.
// We want to use the parent transaction, otherwise the 'afterCommit' hook will never fire in this case.
// See: https://github.com/sequelize/sequelize/issues/17452
(options.transaction.parent || options.transaction).afterCommit(
() => void globalEventQueue.add(model)
);
return;
}
void globalEventQueue.add(model);
+7
View File
@@ -0,0 +1,7 @@
import "sequelize";
declare module "sequelize" {
interface Transaction {
parent?: Transaction;
}
}