How to use nested include in sequelize with where clause?












0














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
}
}









share|improve this question





























    0














    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
    }
    }









    share|improve this question



























      0












      0








      0







      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
      }
      }









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 7:29









      kit

      1,1083616




      1,1083616










      asked Nov 20 at 7:14









      Schüler

      12915




      12915
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I can see few confusions in your question.




          1. According to your question in your Students table I can see you only have columns examId & subject. so how do you link Results table with Students table.


          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,
          }]
          });



          1. second problem I can see is you want an association (link) between your Students table and your Exams table, but your Exams table you only have columns startDate, EndDate, noOfStudents and collage_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. =)






          share|improve this answer





















            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            0














            I can see few confusions in your question.




            1. According to your question in your Students table I can see you only have columns examId & subject. so how do you link Results table with Students table.


            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,
            }]
            });



            1. second problem I can see is you want an association (link) between your Students table and your Exams table, but your Exams table you only have columns startDate, EndDate, noOfStudents and collage_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. =)






            share|improve this answer


























              0














              I can see few confusions in your question.




              1. According to your question in your Students table I can see you only have columns examId & subject. so how do you link Results table with Students table.


              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,
              }]
              });



              1. second problem I can see is you want an association (link) between your Students table and your Exams table, but your Exams table you only have columns startDate, EndDate, noOfStudents and collage_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. =)






              share|improve this answer
























                0












                0








                0






                I can see few confusions in your question.




                1. According to your question in your Students table I can see you only have columns examId & subject. so how do you link Results table with Students table.


                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,
                }]
                });



                1. second problem I can see is you want an association (link) between your Students table and your Exams table, but your Exams table you only have columns startDate, EndDate, noOfStudents and collage_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. =)






                share|improve this answer












                I can see few confusions in your question.




                1. According to your question in your Students table I can see you only have columns examId & subject. so how do you link Results table with Students table.


                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,
                }]
                });



                1. second problem I can see is you want an association (link) between your Students table and your Exams table, but your Exams table you only have columns startDate, EndDate, noOfStudents and collage_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. =)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 5:56









                Pathum Samararathna

                678419




                678419






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Ottavio Pratesi

                    Tricia Helfer

                    15 giugno