How to use nested include in sequelize with where clause?
I would like to fetch the results of specified college,
Exam table has startDate, EndDate, noOfStudents and collage_id.
Students table has examId & subject
result table has studentId, score and grade.
I have tried this below query it returns null for students and none for the exam also it has been returning all data which is present in result DB, I need only belongs to a specified college so any suggestion here, please
results.find({
include: [{
model: models.students,
include: [{
model: models.exam,
as: 'exam',
where:{collage_id:id}
}],
as: 'students'
}]
});
Output:{
"id": 1,
"student_id":2,
"score": 88,
"grade" : B,
"created_at": "2018-11-14T13:38:25.377Z",
"updated_at": "2018-11-14T13:38:25.377Z",
"students": null
}
Expected Output:{
"id": 1,
"student_id":2,
"score": 88,
"grade" : B,
"created_at": "2018-11-14T13:38:25.377Z",
"updated_at": "2018-11-14T13:38:25.377Z",
"students": {
"examId": 2
"subject":
},exam:{
"startDate":Date,
"EndDate":date,
"noOfStudents" : 100
"collage_id": 1
}
}
postgresql include sequelize.js postgresql-9.5 sequelize-cli
add a comment |
I would like to fetch the results of specified college,
Exam table has startDate, EndDate, noOfStudents and collage_id.
Students table has examId & subject
result table has studentId, score and grade.
I have tried this below query it returns null for students and none for the exam also it has been returning all data which is present in result DB, I need only belongs to a specified college so any suggestion here, please
results.find({
include: [{
model: models.students,
include: [{
model: models.exam,
as: 'exam',
where:{collage_id:id}
}],
as: 'students'
}]
});
Output:{
"id": 1,
"student_id":2,
"score": 88,
"grade" : B,
"created_at": "2018-11-14T13:38:25.377Z",
"updated_at": "2018-11-14T13:38:25.377Z",
"students": null
}
Expected Output:{
"id": 1,
"student_id":2,
"score": 88,
"grade" : B,
"created_at": "2018-11-14T13:38:25.377Z",
"updated_at": "2018-11-14T13:38:25.377Z",
"students": {
"examId": 2
"subject":
},exam:{
"startDate":Date,
"EndDate":date,
"noOfStudents" : 100
"collage_id": 1
}
}
postgresql include sequelize.js postgresql-9.5 sequelize-cli
add a comment |
I would like to fetch the results of specified college,
Exam table has startDate, EndDate, noOfStudents and collage_id.
Students table has examId & subject
result table has studentId, score and grade.
I have tried this below query it returns null for students and none for the exam also it has been returning all data which is present in result DB, I need only belongs to a specified college so any suggestion here, please
results.find({
include: [{
model: models.students,
include: [{
model: models.exam,
as: 'exam',
where:{collage_id:id}
}],
as: 'students'
}]
});
Output:{
"id": 1,
"student_id":2,
"score": 88,
"grade" : B,
"created_at": "2018-11-14T13:38:25.377Z",
"updated_at": "2018-11-14T13:38:25.377Z",
"students": null
}
Expected Output:{
"id": 1,
"student_id":2,
"score": 88,
"grade" : B,
"created_at": "2018-11-14T13:38:25.377Z",
"updated_at": "2018-11-14T13:38:25.377Z",
"students": {
"examId": 2
"subject":
},exam:{
"startDate":Date,
"EndDate":date,
"noOfStudents" : 100
"collage_id": 1
}
}
postgresql include sequelize.js postgresql-9.5 sequelize-cli
I would like to fetch the results of specified college,
Exam table has startDate, EndDate, noOfStudents and collage_id.
Students table has examId & subject
result table has studentId, score and grade.
I have tried this below query it returns null for students and none for the exam also it has been returning all data which is present in result DB, I need only belongs to a specified college so any suggestion here, please
results.find({
include: [{
model: models.students,
include: [{
model: models.exam,
as: 'exam',
where:{collage_id:id}
}],
as: 'students'
}]
});
Output:{
"id": 1,
"student_id":2,
"score": 88,
"grade" : B,
"created_at": "2018-11-14T13:38:25.377Z",
"updated_at": "2018-11-14T13:38:25.377Z",
"students": null
}
Expected Output:{
"id": 1,
"student_id":2,
"score": 88,
"grade" : B,
"created_at": "2018-11-14T13:38:25.377Z",
"updated_at": "2018-11-14T13:38:25.377Z",
"students": {
"examId": 2
"subject":
},exam:{
"startDate":Date,
"EndDate":date,
"noOfStudents" : 100
"collage_id": 1
}
}
postgresql include sequelize.js postgresql-9.5 sequelize-cli
postgresql include sequelize.js postgresql-9.5 sequelize-cli
edited Nov 20 at 7:29
kit
1,1083616
1,1083616
asked Nov 20 at 7:14
Schüler
12915
12915
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I can see few confusions in your question.
- According to your question in your
Students tableI can see you only have columnsexamId&subject. so how do you linkResultstable withStudentstable.
To link Results table with Students table your student table should be Students(id / studentId,examId,subject)
and the association (link) results.belongsTo(students,{foreignKey:'id / studentId'})
After doing these this only you can say,
results.find({
include: [{
model: models.students,
}]
});
- second problem I can see is you want an association (link) between your
Studentstable and yourExamstable, but yourExamstable you only have columnsstartDate,EndDate,noOfStudentsandcollage_id. So where does the association here? How do you link student to exam.
I think the column noOfStudents means Number of students. If you mean the id of the student.
You have to have an association Student.hasMany(Exam,{foreignKey:'noOfStudents'})
After doing these to you will be able to query,
results.find({
include: [{
model: models.students,
include: [{
model: models.exam,
where:{collage_id:id}
}]
}]
});
I'm not sure about the keyword as since I am also new to this little bit. as far as I know you have to use the keyword as in your associations before you use it your query though I'm not sure about it.
And also try to trace you query for this (console) and see what it generates. =)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387976%2fhow-to-use-nested-include-in-sequelize-with-where-clause%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I can see few confusions in your question.
- According to your question in your
Students tableI can see you only have columnsexamId&subject. so how do you linkResultstable withStudentstable.
To link Results table with Students table your student table should be Students(id / studentId,examId,subject)
and the association (link) results.belongsTo(students,{foreignKey:'id / studentId'})
After doing these this only you can say,
results.find({
include: [{
model: models.students,
}]
});
- second problem I can see is you want an association (link) between your
Studentstable and yourExamstable, but yourExamstable you only have columnsstartDate,EndDate,noOfStudentsandcollage_id. So where does the association here? How do you link student to exam.
I think the column noOfStudents means Number of students. If you mean the id of the student.
You have to have an association Student.hasMany(Exam,{foreignKey:'noOfStudents'})
After doing these to you will be able to query,
results.find({
include: [{
model: models.students,
include: [{
model: models.exam,
where:{collage_id:id}
}]
}]
});
I'm not sure about the keyword as since I am also new to this little bit. as far as I know you have to use the keyword as in your associations before you use it your query though I'm not sure about it.
And also try to trace you query for this (console) and see what it generates. =)
add a comment |
I can see few confusions in your question.
- According to your question in your
Students tableI can see you only have columnsexamId&subject. so how do you linkResultstable withStudentstable.
To link Results table with Students table your student table should be Students(id / studentId,examId,subject)
and the association (link) results.belongsTo(students,{foreignKey:'id / studentId'})
After doing these this only you can say,
results.find({
include: [{
model: models.students,
}]
});
- second problem I can see is you want an association (link) between your
Studentstable and yourExamstable, but yourExamstable you only have columnsstartDate,EndDate,noOfStudentsandcollage_id. So where does the association here? How do you link student to exam.
I think the column noOfStudents means Number of students. If you mean the id of the student.
You have to have an association Student.hasMany(Exam,{foreignKey:'noOfStudents'})
After doing these to you will be able to query,
results.find({
include: [{
model: models.students,
include: [{
model: models.exam,
where:{collage_id:id}
}]
}]
});
I'm not sure about the keyword as since I am also new to this little bit. as far as I know you have to use the keyword as in your associations before you use it your query though I'm not sure about it.
And also try to trace you query for this (console) and see what it generates. =)
add a comment |
I can see few confusions in your question.
- According to your question in your
Students tableI can see you only have columnsexamId&subject. so how do you linkResultstable withStudentstable.
To link Results table with Students table your student table should be Students(id / studentId,examId,subject)
and the association (link) results.belongsTo(students,{foreignKey:'id / studentId'})
After doing these this only you can say,
results.find({
include: [{
model: models.students,
}]
});
- second problem I can see is you want an association (link) between your
Studentstable and yourExamstable, but yourExamstable you only have columnsstartDate,EndDate,noOfStudentsandcollage_id. So where does the association here? How do you link student to exam.
I think the column noOfStudents means Number of students. If you mean the id of the student.
You have to have an association Student.hasMany(Exam,{foreignKey:'noOfStudents'})
After doing these to you will be able to query,
results.find({
include: [{
model: models.students,
include: [{
model: models.exam,
where:{collage_id:id}
}]
}]
});
I'm not sure about the keyword as since I am also new to this little bit. as far as I know you have to use the keyword as in your associations before you use it your query though I'm not sure about it.
And also try to trace you query for this (console) and see what it generates. =)
I can see few confusions in your question.
- According to your question in your
Students tableI can see you only have columnsexamId&subject. so how do you linkResultstable withStudentstable.
To link Results table with Students table your student table should be Students(id / studentId,examId,subject)
and the association (link) results.belongsTo(students,{foreignKey:'id / studentId'})
After doing these this only you can say,
results.find({
include: [{
model: models.students,
}]
});
- second problem I can see is you want an association (link) between your
Studentstable and yourExamstable, but yourExamstable you only have columnsstartDate,EndDate,noOfStudentsandcollage_id. So where does the association here? How do you link student to exam.
I think the column noOfStudents means Number of students. If you mean the id of the student.
You have to have an association Student.hasMany(Exam,{foreignKey:'noOfStudents'})
After doing these to you will be able to query,
results.find({
include: [{
model: models.students,
include: [{
model: models.exam,
where:{collage_id:id}
}]
}]
});
I'm not sure about the keyword as since I am also new to this little bit. as far as I know you have to use the keyword as in your associations before you use it your query though I'm not sure about it.
And also try to trace you query for this (console) and see what it generates. =)
answered Nov 21 at 5:56
Pathum Samararathna
678419
678419
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387976%2fhow-to-use-nested-include-in-sequelize-with-where-clause%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown