You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, associations are loaded using a left join, that is to say it only includes records from the parent table. You can change this behavior to a right join by passing the `right` property, if the dialect you are using supports it. Currenly, `sqlite` *does not* support [right joins](https://www.sqlite.org/omitted.html).
766
+
767
+
*Note:* `right` is only respected if `required` is false.
768
+
769
+
```js
770
+
User.findAll({
771
+
include: [{
772
+
model: Tool // will create a left join
773
+
}]
774
+
});
775
+
776
+
User.findAll({
777
+
include: [{
778
+
model: Tool,
779
+
right:true// will create a right join
780
+
}]
781
+
});
782
+
783
+
User.findAll({
784
+
include: [{
785
+
model: Tool,
786
+
required:true,
787
+
right:true// has no effect, will create an inner join
788
+
}]
789
+
});
790
+
791
+
User.findAll({
792
+
include: [{
793
+
model: Tool,
794
+
where: { name: { [Op.like]:'%ooth%' } },
795
+
right:true// has no effect, will create an inner join
796
+
}]
797
+
});
798
+
799
+
User.findAll({
800
+
include: [{
801
+
model: Tool,
802
+
where: { name: { [Op.like]:'%ooth%' } },
803
+
required:false
804
+
right:true// because we set `required` to false, this will create a right join
Copy file name to clipboardExpand all lines: lib/model.js
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1653,6 +1653,7 @@ class Model {
1653
1653
* @param {Object} [options.include[].on] Supply your own ON condition for the join.
1654
1654
* @param {Array<string>} [options.include[].attributes] A list of attributes to select from the child model
1655
1655
* @param {boolean} [options.include[].required] If true, converts to an inner join, which means that the parent model will only be loaded if it has any matching children. True if `include.where` is set, false otherwise.
1656
+
* @param {boolean} [options.include[].right] If true, converts to a right join if dialect support it. Ignored if `include.required` is true.
1656
1657
* @param {boolean} [options.include[].separate] If true, runs a separate query to fetch the associated instances, only supported for hasMany associations
1657
1658
* @param {number} [options.include[].limit] Limit the joined rows, only supported with include.separate=true
1658
1659
* @param {Object} [options.include[].through.where] Filter on the join model for belongsToMany relations
default: `SELECT [User].[name], [User].[age], [Posts].[id] AS [Posts.id], [Posts].[title] AS [Posts.title] FROM [User] AS [User] ${current.dialect.supports['RIGHT JOIN'] ? 'RIGHT' : 'LEFT'} OUTER JOIN [Post] AS [Posts] ON [User].[id] = [Posts].[user_id];`
default: `SELECT [user].[id_user], [user].[id], [projects].[id] AS [projects.id], [projects].[title] AS [projects.title], [projects].[createdAt] AS [projects.createdAt], [projects].[updatedAt] AS [projects.updatedAt], [projects->project_user].[user_id] AS [projects.project_user.userId], [projects->project_user].[project_id] AS [projects.project_user.projectId] FROM [User] AS [user] ${current.dialect.supports['RIGHT JOIN'] ? 'RIGHT' : 'LEFT'} OUTER JOIN ( [project_users] AS [projects->project_user] INNER JOIN [projects] AS [projects] ON [projects].[id] = [projects->project_user].[project_id]) ON [user].[id_user] = [projects->project_user].[user_id];`,
448
+
sqlite: `SELECT \`user\`.\`id_user\`, \`user\`.\`id\`, \`projects\`.\`id\` AS \`projects.id\`, \`projects\`.\`title\` AS \`projects.title\`, \`projects\`.\`createdAt\` AS \`projects.createdAt\`, \`projects\`.\`updatedAt\` AS \`projects.updatedAt\`, \`projects->project_user\`.\`user_id\` AS \`projects.project_user.userId\`, \`projects->project_user\`.\`project_id\` AS \`projects.project_user.projectId\` FROM \`User\` AS \`user\` ${current.dialect.supports['RIGHT JOIN'] ? 'RIGHT' : 'LEFT'} OUTER JOIN \`project_users\` AS \`projects->project_user\` ON \`user\`.\`id_user\` = \`projects->project_user\`.\`user_id\` LEFT OUTER JOIN \`projects\` AS \`projects\` ON \`projects\`.\`id\` = \`projects->project_user\`.\`project_id\`;`
0 commit comments