diff --git a/docs/core-concepts/assocs.md b/docs/core-concepts/assocs.md index ef6a99831..2e5aa966c 100644 --- a/docs/core-concepts/assocs.md +++ b/docs/core-concepts/assocs.md @@ -680,16 +680,55 @@ Practical demonstration: await Bar.findOne({ include: Foo }); ``` -## Multiple associations involving the same models +## Multiple associations between the same models In Sequelize, it is possible to define multiple associations between the same models. You just have to define different aliases for them: -```js -Team.hasOne(Game, { as: 'HomeTeam', foreignKey: 'homeTeamId' }); -Team.hasOne(Game, { as: 'AwayTeam', foreignKey: 'awayTeamId' }); +```typescript +const Team = sequelize.define('team', { + name: DataTypes.TEXT, +}, { timestamps: true }); +const Game = sequelize.define('game', { + name: DataTypes.TEXT, +}, { timestamps: true }); + +Team.hasOne(Game, { as: 'HomeGame', foreignKey: 'homeGameId' }); +Team.hasOne(Game, { as: 'AwayGame', foreignKey: 'awayGameId' }); Game.belongsTo(Team); ``` +Now you able to use the associations with Lazy Loading: + +```typescript +// Find the home team name +const awesomeHomeTeam = await Team.findOne({ + where: { + name: "Home Ball Team" + }, +}); + +// Find the home team game +const selectedHomeTeamGame = await awesomeHomeTeam.getHomeGame(); + +console.log('Team Name:', awesomeHomeTeam.name); // Home Ball Team +console.log('Team Game Name:', selectedHomeTeamGame.name); // Home Ball Game +``` + +or with Eager Loading: + +```typescript +// Find the home team name +const awesomeHomeTeam = await Team.findOne({ + where: { + name: "Home Ball Team" + }, + include: Game, // Add `include` option to use eager loading +}); + +console.log('Team Name:', awesomeHomeTeam.name); // Home Ball Team +console.log('Team Game Name:', awesomeHomeTeam.game.name); // Home Ball Game +``` + ## Creating associations referencing a field which is not the primary key In all the examples above, the associations were defined by referencing the primary keys of the involved models (in our case, their IDs). However, Sequelize allows you to define an association that uses another field, instead of the primary key field, to establish the association.