innerJoin
The innerJoin method enables you to join associations as part of your queries.
const hosts = await Host.innerJoin('places', 'rooms').all()
select "hosts".* from "hosts" inner join "places" on "hosts"."id" = "places"."host_id" inner join "rooms" on "places"."id" = "rooms"."place_id"
You can also attach and
, andNot
, and andAny
clauses to join statements. The second argument (the object) in the following code generates an and
condition on the join:
const hosts = await Host.innerJoin('places', {
and: { name: 'Mountain Cabin' },
}).all()
select "hosts".* from "hosts" inner join "places" on "hosts"."id" = "places"."host_id" and "places"."name" = $1
// An `and` statement on both `places` and `rooms`
const hosts = await Host.innerJoin(
'places',
{ and: { style: 'cabin' } },
'rooms',
{ and: { type: 'Bedroom' } }
).all()
// An `and` statement on just `rooms`
const hosts = await Host.innerJoin('places', 'rooms', {
and: { type: 'Bedroom' },
}).all()