0
votes

I have this piece of code,

and my after create hook is executing DB queries in the wrong order


afterCreate: async (instance1, { transaction }) => {
    const model2 = this.db.getModel('Model2');

    // PART 1
    const instance2 = await model2.findOne({
    where: { id: instance.model2Id },
    transaction,
    });

    // PART 2
    return mission.update({...}, { transaction });
}

When using BulkCreate(2 items) I have no idea why order of query execution is,

PART 1(first) -> PART 1(second) -> PART 2(first) -> PART 2(second).

Any hints?

Thanks a lot.

1
Hello, I remember, looking in the code of sequelize that they use a Promise.map in order to handle the multiple instances passed to bulkCreate. A promise.map wait for all promises to resolve. This means that the promise are executed at the same time in parallel. So the order you describe, seems logic regarding that. - Robin Biondi

1 Answers

0
votes

Looks like you're not waiting for the update to finish. Try:

// PART 2
    return await mission.update({...}, { transaction });