# Join Methods

# joinRelated()

queryBuilder = queryBuilder.joinRelated(relationExpression, opt);
Copied!

Joins a set of relations described by relationExpression. See the examples for more info.

# Arguments
Argument Type Description
relationExpression RelationExpression An expression describing which relations to join.
opt object Optional options. See the examples.
# Return value
Type Description
QueryBuilder this query builder for chaining.
# Examples

Join one relation:

await Person.query()
  .joinRelated('pets')
  .where('pets.species', 'dog');
Copied!

Give an alias for a single relation:

await Person.query()
  .joinRelated('pets', { alias: 'p' })
  .where('p.species', 'dog');
Copied!

Join two relations:

await Person.query()
  .joinRelated('[pets, parent]')
  .where('pets.species', 'dog')
  .where('parent.name', 'Arnold');
Copied!

You can also use the object notation

await Person.query()
  .joinRelated({
    pets: true,
    parent: true
  })
  .where('pets.species', 'dog')
  .where('parent.name', 'Arnold');
Copied!

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');
Copied!

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');
Copied!

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');
Copied!

# innerJoinRelated()

Alias for joinRelated.

# outerJoinRelated()

Outer join version of the joinRelated method.

# leftJoinRelated()

Left join version of the joinRelated method.

# leftOuterJoinRelated()

Left outer join version of the joinRelated method.

# rightJoinRelated()

Right join version of the joinRelated method.

# rightOuterJoinRelated()

Left outer join version of the joinRelated method.

# fullOuterJoinRelated()

Full outer join version of the joinRelated method.

# join()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.

# joinRaw()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.

# innerJoin()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.

# leftJoin()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.

# leftOuterJoin()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.

# rightJoin()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.

# rightOuterJoin()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.

# outerJoin()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.

# fullOuterJoin()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.

# crossJoin()

See knex documentation (opens new window)

# Return value
Type Description
QueryBuilder this query builder for chaining.