Gabriel Yamin
Gabriel Yamin

Reputation: 95

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!

Upvotes: 2

Views: 1471

Answers (2)

depankur rawat
depankur rawat

Reputation: 17

Pass the transaction which you had used at the time of insertion(or create) as:

const models = await connectToDatabase();
return models.model.findAll({
 include: {
  all: true
 }
 transaction: transaction
});

Upvotes: 0

Gabriel Yamin
Gabriel Yamin

Reputation: 95

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.

Upvotes: 6

Related Questions