@@ -759,3 +759,49 @@ Include all also supports nested loading:
```js
User.findAll({include:[{all:true,nested:true}]});
```
### Use right join for association
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).
*Note:*`right` is only respected if `required` is false.
```js
User.findAll({
include:[{
model:Tool// will create a left join
}]
});
User.findAll({
include:[{
model:Tool,
right:true// will create a right join
}]
});
User.findAll({
include:[{
model:Tool,
required:true,
right:true// has no effect, will create an inner join
}]
});
User.findAll({
include:[{
model:Tool,
where:{name:{[Op.like]:'%ooth%'}},
right:true// has no effect, will create an inner join
}]
});
User.findAll({
include:[{
model:Tool,
where:{name:{[Op.like]:'%ooth%'}},
required:false
right:true// because we set `required` to false, this will create a right join
* @param {Object} [options.include[].on] Supply your own ON condition for the join.
* @param {Array<string>} [options.include[].attributes] A list of attributes to select from the child model
* @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.
* @param {boolean} [options.include[].right] If true, converts to a right join if dialect support it. Ignored if `include.required` is true.
* @param {boolean} [options.include[].separate] If true, runs a separate query to fetch the associated instances, only supported for hasMany associations
* @param {number} [options.include[].limit] Limit the joined rows, only supported with include.separate=true
* @param {Object} [options.include[].through.where] Filter on the join model for belongsToMany relations
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];`,
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\`;`