mysqlnode.jssequelize.js

findAll() in Sequelize doesn't find a row created inside a transaction


I'm having a problem when creating a row and later on selecting it while still under the same transaction.

Whenever I execute the following code I usually get an empty array and rarely get an array with the newly created row.

This is a sample of the code I run:

return seq.transaction((tx) => {
    const options = {transaction: tx};
    return model.create(user, options)
    .then(() => {
        return model.findAll({
            include: {
                all: true
            }
    }, options)
    .then((data) => ({
        console.log(data);
    });
});

Is there something basic I'm missing here? Thanks!


Solution

  • I solved the problem. What I did wrong was giving the transaction to the findAll() function as an additional argument instead of part of the first argument's JSON.

    So instead of:

    return model.findAll({
        include: {
            all: true
        }
    }, options)
    

    I should've done:

    return model.findAll({
        include: {
            all: true
        },
        transaction: options.transaction
    });
    

    Hope this solution will be helpful for other people.