javascript - knex js - one to many relation - Stack Overflow

I am having two tables - Questions and Answers which are connected by foreign key on Answers named ques

I am having two tables - Questions and Answers which are connected by foreign key on Answers named questionId. Each question may contain multiple answers. I am trying to create a single query to obtain questions with all fields and put answers into field of type array. For instance this is the question structure:

{
id: 5
name: "abc",
answers: ["a","b","c"]
}

How can I force knex to group by questionId and put all of them to answers? I tried to use leftjoins but it doesn't work for one to many relationships. This is what I've tried:

 var questionQuery = this.knex.select().table(this.questionWithAnswersTb)
            .select("*")
            .leftJoin(this.answersTb, this.questionWithAnswersTb + ".id", this.answersTb + ".questionId");

I am having two tables - Questions and Answers which are connected by foreign key on Answers named questionId. Each question may contain multiple answers. I am trying to create a single query to obtain questions with all fields and put answers into field of type array. For instance this is the question structure:

{
id: 5
name: "abc",
answers: ["a","b","c"]
}

How can I force knex to group by questionId and put all of them to answers? I tried to use leftjoins but it doesn't work for one to many relationships. This is what I've tried:

 var questionQuery = this.knex.select().table(this.questionWithAnswersTb)
            .select("*")
            .leftJoin(this.answersTb, this.questionWithAnswersTb + ".id", this.answersTb + ".questionId");
Share Improve this question edited Dec 22, 2015 at 17:20 MistyK asked Dec 22, 2015 at 17:14 MistyKMistyK 6,2725 gold badges47 silver badges81 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

I would approach this by doing multiple queries. I would first query for the question's that I want, and you can make your initial questionObjects array with this information that has the name and id properties. Then you can iteratively call something like:

var questionObjects = [{id: 5, name: abc}, {id: 6, name: xyz}]; 
// first query makes something like this array ^^, then..
questionObjects.forEach(function(question, index) {
  this.knex.select('answersTable.answers').from('questionsTable').leftJoin(
  'answersTable',
  'answersTable.questionId', 
  'questionsTable.id').where('answersTable.questionId', question.id})
  .then(function(answers) {
    answers = answers.map(/*make your answers look the way you want...*/)
    questionObjects[index].answers = answers;  
  })

That last bit in the .then() callback may require some additional manipulation of the return value from the previous query to get the answers in the array format you ultimately want.

You can make sql-views from the rdbms for your one-to-many or many-to-many relationships you will have. and then query from those views. Its easier that way if you are going to reuse those relationship using knex. For insertion and updating use one knex transaction on the tables.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745548585a4632457.html

相关推荐

  • javascript - knex js - one to many relation - Stack Overflow

    I am having two tables - Questions and Answers which are connected by foreign key on Answers named ques

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信