How to use transactions in LUCID ORM on update

#1

How to pass Transactions (trx) on LUCID update

My sample code

const Database = use('Database');
const User = use('App/Models/User');
const trx = await Database.beginTransaction();

await User.query()
        .where(id, 1)
        .update({name : 'john'});

try{
await User.query()
        .where(id, 2)
        .update({name : 'Doe'});

await trx.commit();
}
catch(err){
await trx.rollback();
}

Adonis version 4.1

0 Likes

#2

Did you try to pass the transaction as a second parameter to the update method? @mritgupta

await User.query()
        .where(id, 1)
        .update({name : 'john'}, trx);
0 Likes

#3

Please read knex docs too, otherwise you will keep on assuming things.

await User.query()
        .where(id, 1)
        .transacting(trx)
        .update({name : 'john'})
2 Likes