Get values from associated table with Sequelize.js









up vote
11
down vote

favorite
4












team table match table
=========== ================================
tid= name mid= date =home_team=away_team
============= ================================
01 = denver 01 =10.11.13 = 01 = 04
02 = minesota 02 =11.11.13 = 02 = 03
03 = orlando 03 =11.11.13 = 04 = 02
04 = portland 04 =12.11.13 = 03 = 01


I have a classical SQL JOIN problem - filled the match data and can't get the names of home and away teams that located in another table.



var Team = sequelize.define('Team', ... );
var Match = sequelize.define('Match', .. );

Team.hasOne(Match, foreignKey: 'home_team', as: 'Home')
Team.hasOne(Match, foreignKey: 'away_team', as: 'Away');


As i understood from Docs after creating as: 'Home and as: 'Away i receive some
getters and setters like Match.getHome but i'm confused. how can i used it



Match.find(where: id: 1).success(function(match) 
console.log(match);
);









share|improve this question



























    up vote
    11
    down vote

    favorite
    4












    team table match table
    =========== ================================
    tid= name mid= date =home_team=away_team
    ============= ================================
    01 = denver 01 =10.11.13 = 01 = 04
    02 = minesota 02 =11.11.13 = 02 = 03
    03 = orlando 03 =11.11.13 = 04 = 02
    04 = portland 04 =12.11.13 = 03 = 01


    I have a classical SQL JOIN problem - filled the match data and can't get the names of home and away teams that located in another table.



    var Team = sequelize.define('Team', ... );
    var Match = sequelize.define('Match', .. );

    Team.hasOne(Match, foreignKey: 'home_team', as: 'Home')
    Team.hasOne(Match, foreignKey: 'away_team', as: 'Away');


    As i understood from Docs after creating as: 'Home and as: 'Away i receive some
    getters and setters like Match.getHome but i'm confused. how can i used it



    Match.find(where: id: 1).success(function(match) 
    console.log(match);
    );









    share|improve this question

























      up vote
      11
      down vote

      favorite
      4









      up vote
      11
      down vote

      favorite
      4






      4





      team table match table
      =========== ================================
      tid= name mid= date =home_team=away_team
      ============= ================================
      01 = denver 01 =10.11.13 = 01 = 04
      02 = minesota 02 =11.11.13 = 02 = 03
      03 = orlando 03 =11.11.13 = 04 = 02
      04 = portland 04 =12.11.13 = 03 = 01


      I have a classical SQL JOIN problem - filled the match data and can't get the names of home and away teams that located in another table.



      var Team = sequelize.define('Team', ... );
      var Match = sequelize.define('Match', .. );

      Team.hasOne(Match, foreignKey: 'home_team', as: 'Home')
      Team.hasOne(Match, foreignKey: 'away_team', as: 'Away');


      As i understood from Docs after creating as: 'Home and as: 'Away i receive some
      getters and setters like Match.getHome but i'm confused. how can i used it



      Match.find(where: id: 1).success(function(match) 
      console.log(match);
      );









      share|improve this question















      team table match table
      =========== ================================
      tid= name mid= date =home_team=away_team
      ============= ================================
      01 = denver 01 =10.11.13 = 01 = 04
      02 = minesota 02 =11.11.13 = 02 = 03
      03 = orlando 03 =11.11.13 = 04 = 02
      04 = portland 04 =12.11.13 = 03 = 01


      I have a classical SQL JOIN problem - filled the match data and can't get the names of home and away teams that located in another table.



      var Team = sequelize.define('Team', ... );
      var Match = sequelize.define('Match', .. );

      Team.hasOne(Match, foreignKey: 'home_team', as: 'Home')
      Team.hasOne(Match, foreignKey: 'away_team', as: 'Away');


      As i understood from Docs after creating as: 'Home and as: 'Away i receive some
      getters and setters like Match.getHome but i'm confused. how can i used it



      Match.find(where: id: 1).success(function(match) 
      console.log(match);
      );






      sequelize.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 12:58









      Cœur

      17.2k9102141




      17.2k9102141










      asked Nov 22 '13 at 22:57









      khex

      1,36062141




      1,36062141






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          16
          down vote



          accepted










          The problem lies in your association. You have only defined the assocation from team to match, but now you want to go the other way, from match to team. This means you have to do:



          Match.belongsTo(Team, foreignKey: 'home_team', as: 'Home');
          Match.belongsTo(Team, foreignKey: 'away_team', as: 'Away');


          After that you can do



          Match.find(where: mid: 1).success(function(match) 
          match.getHome().success(function(home_team)

          );
          );


          Or you can use eager loading:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home
          );


          If you want both the home and away team at once:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          model: Team, as: 'Away'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home and away team in match.away
          );





          share|improve this answer






















          • Thanks a lot. I'm very confused with all of these .hasOne() and .belongsTo()
            – khex
            Nov 23 '13 at 16:22






          • 3




            You are not the only one, we often get questions about those. There is a TODO in the docs to make the types of assocations clearer, you can add your voice there if you want to github.com/sequelize/sequelize-doc/issues/80 (I am a sequelize maintainer, that's why I know the associations so well :) )
            – Jan Aagaard Meier
            Nov 23 '13 at 17:04







          • 1




            I'm not really sure what you mean by that last comment? Could you perhaps update your question with the result you want to achieve?
            – Jan Aagaard Meier
            Nov 24 '13 at 22:28






          • 1




            nothing, I found the answer in your previous answer bit.ly/IdHfV4
            – khex
            Nov 25 '13 at 21:23










          • perfect answer, I almost give up and this show related table, thank you
            – Faisal
            Dec 11 '17 at 13:16










          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',
          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%2f20156045%2fget-values-from-associated-table-with-sequelize-js%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








          up vote
          16
          down vote



          accepted










          The problem lies in your association. You have only defined the assocation from team to match, but now you want to go the other way, from match to team. This means you have to do:



          Match.belongsTo(Team, foreignKey: 'home_team', as: 'Home');
          Match.belongsTo(Team, foreignKey: 'away_team', as: 'Away');


          After that you can do



          Match.find(where: mid: 1).success(function(match) 
          match.getHome().success(function(home_team)

          );
          );


          Or you can use eager loading:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home
          );


          If you want both the home and away team at once:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          model: Team, as: 'Away'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home and away team in match.away
          );





          share|improve this answer






















          • Thanks a lot. I'm very confused with all of these .hasOne() and .belongsTo()
            – khex
            Nov 23 '13 at 16:22






          • 3




            You are not the only one, we often get questions about those. There is a TODO in the docs to make the types of assocations clearer, you can add your voice there if you want to github.com/sequelize/sequelize-doc/issues/80 (I am a sequelize maintainer, that's why I know the associations so well :) )
            – Jan Aagaard Meier
            Nov 23 '13 at 17:04







          • 1




            I'm not really sure what you mean by that last comment? Could you perhaps update your question with the result you want to achieve?
            – Jan Aagaard Meier
            Nov 24 '13 at 22:28






          • 1




            nothing, I found the answer in your previous answer bit.ly/IdHfV4
            – khex
            Nov 25 '13 at 21:23










          • perfect answer, I almost give up and this show related table, thank you
            – Faisal
            Dec 11 '17 at 13:16














          up vote
          16
          down vote



          accepted










          The problem lies in your association. You have only defined the assocation from team to match, but now you want to go the other way, from match to team. This means you have to do:



          Match.belongsTo(Team, foreignKey: 'home_team', as: 'Home');
          Match.belongsTo(Team, foreignKey: 'away_team', as: 'Away');


          After that you can do



          Match.find(where: mid: 1).success(function(match) 
          match.getHome().success(function(home_team)

          );
          );


          Or you can use eager loading:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home
          );


          If you want both the home and away team at once:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          model: Team, as: 'Away'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home and away team in match.away
          );





          share|improve this answer






















          • Thanks a lot. I'm very confused with all of these .hasOne() and .belongsTo()
            – khex
            Nov 23 '13 at 16:22






          • 3




            You are not the only one, we often get questions about those. There is a TODO in the docs to make the types of assocations clearer, you can add your voice there if you want to github.com/sequelize/sequelize-doc/issues/80 (I am a sequelize maintainer, that's why I know the associations so well :) )
            – Jan Aagaard Meier
            Nov 23 '13 at 17:04







          • 1




            I'm not really sure what you mean by that last comment? Could you perhaps update your question with the result you want to achieve?
            – Jan Aagaard Meier
            Nov 24 '13 at 22:28






          • 1




            nothing, I found the answer in your previous answer bit.ly/IdHfV4
            – khex
            Nov 25 '13 at 21:23










          • perfect answer, I almost give up and this show related table, thank you
            – Faisal
            Dec 11 '17 at 13:16












          up vote
          16
          down vote



          accepted







          up vote
          16
          down vote



          accepted






          The problem lies in your association. You have only defined the assocation from team to match, but now you want to go the other way, from match to team. This means you have to do:



          Match.belongsTo(Team, foreignKey: 'home_team', as: 'Home');
          Match.belongsTo(Team, foreignKey: 'away_team', as: 'Away');


          After that you can do



          Match.find(where: mid: 1).success(function(match) 
          match.getHome().success(function(home_team)

          );
          );


          Or you can use eager loading:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home
          );


          If you want both the home and away team at once:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          model: Team, as: 'Away'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home and away team in match.away
          );





          share|improve this answer














          The problem lies in your association. You have only defined the assocation from team to match, but now you want to go the other way, from match to team. This means you have to do:



          Match.belongsTo(Team, foreignKey: 'home_team', as: 'Home');
          Match.belongsTo(Team, foreignKey: 'away_team', as: 'Away');


          After that you can do



          Match.find(where: mid: 1).success(function(match) 
          match.getHome().success(function(home_team)

          );
          );


          Or you can use eager loading:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home
          );


          If you want both the home and away team at once:



          Match.find(
          where: mid: 1 ,
          include: [
          model: Team, as: 'Home'
          model: Team, as: 'Away'
          ]
          ).success(function(match)
          // Here you can access the home team data in match.home and away team in match.away
          );






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '13 at 17:06

























          answered Nov 23 '13 at 15:46









          Jan Aagaard Meier

          15k25853




          15k25853











          • Thanks a lot. I'm very confused with all of these .hasOne() and .belongsTo()
            – khex
            Nov 23 '13 at 16:22






          • 3




            You are not the only one, we often get questions about those. There is a TODO in the docs to make the types of assocations clearer, you can add your voice there if you want to github.com/sequelize/sequelize-doc/issues/80 (I am a sequelize maintainer, that's why I know the associations so well :) )
            – Jan Aagaard Meier
            Nov 23 '13 at 17:04







          • 1




            I'm not really sure what you mean by that last comment? Could you perhaps update your question with the result you want to achieve?
            – Jan Aagaard Meier
            Nov 24 '13 at 22:28






          • 1




            nothing, I found the answer in your previous answer bit.ly/IdHfV4
            – khex
            Nov 25 '13 at 21:23










          • perfect answer, I almost give up and this show related table, thank you
            – Faisal
            Dec 11 '17 at 13:16
















          • Thanks a lot. I'm very confused with all of these .hasOne() and .belongsTo()
            – khex
            Nov 23 '13 at 16:22






          • 3




            You are not the only one, we often get questions about those. There is a TODO in the docs to make the types of assocations clearer, you can add your voice there if you want to github.com/sequelize/sequelize-doc/issues/80 (I am a sequelize maintainer, that's why I know the associations so well :) )
            – Jan Aagaard Meier
            Nov 23 '13 at 17:04







          • 1




            I'm not really sure what you mean by that last comment? Could you perhaps update your question with the result you want to achieve?
            – Jan Aagaard Meier
            Nov 24 '13 at 22:28






          • 1




            nothing, I found the answer in your previous answer bit.ly/IdHfV4
            – khex
            Nov 25 '13 at 21:23










          • perfect answer, I almost give up and this show related table, thank you
            – Faisal
            Dec 11 '17 at 13:16















          Thanks a lot. I'm very confused with all of these .hasOne() and .belongsTo()
          – khex
          Nov 23 '13 at 16:22




          Thanks a lot. I'm very confused with all of these .hasOne() and .belongsTo()
          – khex
          Nov 23 '13 at 16:22




          3




          3




          You are not the only one, we often get questions about those. There is a TODO in the docs to make the types of assocations clearer, you can add your voice there if you want to github.com/sequelize/sequelize-doc/issues/80 (I am a sequelize maintainer, that's why I know the associations so well :) )
          – Jan Aagaard Meier
          Nov 23 '13 at 17:04





          You are not the only one, we often get questions about those. There is a TODO in the docs to make the types of assocations clearer, you can add your voice there if you want to github.com/sequelize/sequelize-doc/issues/80 (I am a sequelize maintainer, that's why I know the associations so well :) )
          – Jan Aagaard Meier
          Nov 23 '13 at 17:04





          1




          1




          I'm not really sure what you mean by that last comment? Could you perhaps update your question with the result you want to achieve?
          – Jan Aagaard Meier
          Nov 24 '13 at 22:28




          I'm not really sure what you mean by that last comment? Could you perhaps update your question with the result you want to achieve?
          – Jan Aagaard Meier
          Nov 24 '13 at 22:28




          1




          1




          nothing, I found the answer in your previous answer bit.ly/IdHfV4
          – khex
          Nov 25 '13 at 21:23




          nothing, I found the answer in your previous answer bit.ly/IdHfV4
          – khex
          Nov 25 '13 at 21:23












          perfect answer, I almost give up and this show related table, thank you
          – Faisal
          Dec 11 '17 at 13:16




          perfect answer, I almost give up and this show related table, thank you
          – Faisal
          Dec 11 '17 at 13:16

















          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%2f20156045%2fget-values-from-associated-table-with-sequelize-js%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo