doc/api/query-builder/join-methods.md
queryBuilder = queryBuilder.joinRelated(relationExpression, opt);
Joins a set of relations described by relationExpression. See the examples for more info.
| Argument | Type | Description |
|---|---|---|
| relationExpression | RelationExpression | An expression describing which relations to join. |
| opt | object | Optional options. See the examples. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
Join one relation:
await Person.query()
.joinRelated('pets')
.where('pets.species', 'dog');
Give an alias for a single relation:
await Person.query()
.joinRelated('pets', { alias: 'p' })
.where('p.species', 'dog');
Join two relations:
await Person.query()
.joinRelated('[pets, parent]')
.where('pets.species', 'dog')
.where('parent.name', 'Arnold');
You can also use the object notation
await Person.query()
.joinRelated({
pets: true,
parent: true
})
.where('pets.species', 'dog')
.where('parent.name', 'Arnold');
Join multiple nested relations. Note that when referring to nested relations : must be used as a separator instead of .. This limitation comes from the way knex parses table references.
await Person.query()
.select('persons.id', 'parent:parent.name as grandParentName')
.joinRelated('[pets, parent.[pets, parent]]')
.where('parent:pets.species', 'dog');
Give aliases for a bunch of relations:
await Person.query()
.select('persons.id', 'pr:pr.name as grandParentName')
.joinRelated('[pets, parent.[pets, parent]]', {
aliases: {
parent: 'pr',
pets: 'pt'
}
})
.where('pr:pt.species', 'dog');
You can also give aliases using the relation expression:
await Person.query()
.select('persons.id', 'pr:pr.name as grandParentName')
.joinRelated('[pets as pt, parent as pr.[pets as pt, parent as pr]]')
.where('pr:pt.species', 'dog');
Alias for joinRelated.
Outer join version of the joinRelated method.
Left join version of the joinRelated method.
Left outer join version of the joinRelated method.
Right join version of the joinRelated method.
Left outer join version of the joinRelated method.
Full outer join version of the joinRelated method.
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |
| Type | Description |
|---|---|
| QueryBuilder | this query builder for chaining. |