From 5d26dbdd478ea600a375bcfb5a8117ba713b9d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98?= Date: Sun, 2 Oct 2022 14:42:48 +0700 Subject: [PATCH 1/3] fix: misleading aliases in "Multiple associations between the same models" documentation Closes #87 --- docs/core-concepts/assocs.md | 47 +++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/docs/core-concepts/assocs.md b/docs/core-concepts/assocs.md index ef6a9983..56069dd8 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 +}); + +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. From 4905a4d080a4ccbd2908533929909102760c7e28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98?= Date: Sun, 2 Oct 2022 14:47:19 +0700 Subject: [PATCH 2/3] docs: tip comment --- docs/core-concepts/assocs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core-concepts/assocs.md b/docs/core-concepts/assocs.md index 56069dd8..a4ca4f06 100644 --- a/docs/core-concepts/assocs.md +++ b/docs/core-concepts/assocs.md @@ -722,7 +722,7 @@ const awesomeHomeTeam = await Team.findOne({ where: { name: "Home Ball Team" }, - include: Game + include: Game // Add `include` option to use eager loading }); console.log('Team Name:', awesomeHomeTeam.name); // Home Ball Team From 2ca340ac3c4bdfe4440af30b2cd0cfa228897b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98?= Date: Sun, 2 Oct 2022 14:48:13 +0700 Subject: [PATCH 3/3] meta tweaks --- docs/core-concepts/assocs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core-concepts/assocs.md b/docs/core-concepts/assocs.md index a4ca4f06..2e5aa966 100644 --- a/docs/core-concepts/assocs.md +++ b/docs/core-concepts/assocs.md @@ -722,7 +722,7 @@ const awesomeHomeTeam = await Team.findOne({ where: { name: "Home Ball Team" }, - include: Game // Add `include` option to use eager loading + include: Game, // Add `include` option to use eager loading }); console.log('Team Name:', awesomeHomeTeam.name); // Home Ball Team