Get values from associated table with Sequelize.js
up vote
11
down vote
favorite
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
add a comment |
up vote
11
down vote
favorite
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
add a comment |
up vote
11
down vote
favorite
up vote
11
down vote
favorite
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
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
sequelize.js
edited Nov 10 at 12:58
Cœur
17.2k9102141
17.2k9102141
asked Nov 22 '13 at 22:57
khex
1,36062141
1,36062141
add a comment |
add a comment |
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
);
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
add a comment |
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
);
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
add a comment |
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
);
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
add a comment |
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
);
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
);
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
add a comment |
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
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%2f20156045%2fget-values-from-associated-table-with-sequelize-js%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