Node.js Sequelize ManyToMany relationships producing incorrect SQL
I am having a problem with the continuation of a ManyToMany relationship.
Here are my models ...
var db = {
players: sequelize.define('players', {
name: Sequelize.STRING
}),
teams: sequelize.define('teams', {
name: Sequelize.STRING
}),
init: function() {
this.players.hasMany(this.teams, {joinTableName: 'teams_has_players'});
this.teams.hasMany(this.players, {joinTableName: 'teams_has_players'});
this.players.sync();
this.teams.sync();
}
};
Search here
db.players.findAll({
where: {team_id: 1},
include: ['teams']
}).success(function(results) {
// print the results
});
The above link will generate the following SQL:
SELECT
players . *,
teams.name AS `teams.name`,
teams.id AS `teams.id`
FROM
players
LEFT OUTER JOIN
teams_has_players ON teams_has_players.player_id = players.id
LEFT OUTER JOIN
teams ON teams.id = teams_has_players.team_id
WHERE
players.team_id = '1';
What seems to be wrong is that the WHERE clause should be WHERE teams.team_id = '1'
Where am I going wrong?
Thank you in advance
source to share
Hmm, it actually looks pretty normal. db.players.findAll
s where: { team_id: 1 }
should create a request with WHERE players.team_id = '1'
. This was perfectly expected. Also, you will teams
instead have team_id
but id
. However, there is a good chance that include
atm will be broken.
source to share