Return records based on a condition

I need to make a query that verify:

if type_answer is equal to Multipla Escolha so i only have to return the records that are not null in correct_answer_description_id
If the type_answer is not equal to Multipla Escolha, don’t make this rule.

So, i try this way:

async getIdQuestoesDisponveis(idUnidade) {

  return await BookUnitQuestion.query()



    .where("student_id", idEstudante)

    .where("book_unit_id", idUnidade)

    .where("status", false)


    .where("type_answer", "Multipla Escolha")




But i’m getting a infinite loop.
How i can make this?

In sql, i can this result by this query:

SELECT * FROM book_unit_question
WHERE book_unit_id = 2 
AND status = false
AND (type_answer is null or type_answer != 'Multipla Escolha' or
correct_answer_description_id IS NOT NULL)

I don’t know if is the best way, but i fixed this way:

return await Database




   .where('book_unit_id', idUnidade)

   .where('status', false)

   .whereRaw('(type_answer is null or type_answer != ? or correct_answer_description_id IS NOT NULL)', ['Multipla Escolha'])

1 Like