admin管理员组文章数量:1430077
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
2 Answers
Reset to default 2I 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.
本文标签: javascriptknex jsone to many relationStack Overflow
版权声明:本文标题:javascript - knex js - one to many relation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745548584a2662823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论