最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论