Knex Adonis Query: Transaction query already complete

I need to make a transaction:

First i create a new bookUnitQuestionOption, after i created i need to update other table (bookUnitQuestion) with the id that are saved.

I try something like:

async store ({ request }){
     const bookUnitQuestionOption = {...request.all(), user_id: request.user_id};
     const trx = await Database.beginTransaction()
     const responseInsercaoOption = BookUnitQuestionOption.create(bookUnitQuestionOption, trx)
     if(bookUnitQuestionOption.correct === true)
            await this.atualizaOptionCorreta(bookUnitQuestionOption.book_unit_question_id, responseInsercaoOption.id, trx)

     trx.commit() 
}

async atualizaOptionCorreta(bookUnitQuestionId, idOpcaoPergunta, transaction){
    await BookUnitQuestion.query()
        .where('id', bookUnitQuestionId)
        .update({correct_answer_description_id :idOpcaoPergunta}, transaction);
}

But i’m getting a 500 error:

message: “Transaction query already complete, run with DEBUG=knex:tx
for more info”

1 Like

Hi!
Seems like await is missing from here.

Also run with DEBUG=knex:tx to get output that describes more what’s wrong