0

I have the following database structure: 2 table DB structure

The questions table is linked with the answers table through answers.questionId references questions.id.

The self-referenced table is referenced through answers.parentId references answers.id.

For example, I have the following data:

Questions table:

questions table entries

Answers table

answers table entries

Is it possible to get something like this (by querying with typeor):

[{id: 1, question: "What is my name?", answers: [{ value: "Man" }, { value: "Boss"}] },
 {id: 2, question: "Where I am?", answers: [*{ value: "Country", answers: [{ value: "Ro" }, { value: "En" }] }*, {value:"Iland"}]

I want to have the answers self-referenced relations as well.

I tried this:

 this.questionsRepo.find({
  relations: ['answers'],
});

But obviously I get only the question and answer array

Is it possible to do something like that with an orm like TypeOrm?

Thanks for reading this!

1 Answer 1

2

I manage to fix it. If somebody have similar issues, here is the code:

this.questionsRepo
      .createQueryBuilder('questions')
      .leftJoinAndSelect(
        'questions.answers',
        'answer',
        'answer.parentId is NULL',
      )
      .leftJoinAndSelect('answer.children', 'answer2')
      .getMany();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.