Expand description
Many-to-many relations use a join model with a has_many relation on each
endpoint and a has_many(via = ...) relation for direct traversal.
Functionsยง
- composite_
join_ key_ prevents_ duplicate_ links - A composite primary key on the join model prevents duplicate links while allowing each endpoint to participate in other links.
- filter_
through_ join_ model - Parent queries can use
.any()on the join relation. The predicate can traverse to the opposite endpoint and can also inspect fields stored on the join model. - filter_
through_ via_ from_ both_ sides - Parent queries can apply
.any()directly to either derived relation, without spelling the join model in the predicate. - include_
from_ both_ sides - Preloading works in both directions and keeps results grouped under the correct endpoint, including endpoints with no join rows.
- mutate_
link_ through_ join_ model - Links are created, updated, and removed by mutating the join model. This
preserves relation data such as
roleand leaves both endpoint records intact when the link is removed. - query_
from_ both_ sides - Both endpoints can traverse the same join rows. The derived query retains normal query operations such as ordering and returns an empty list when no join rows exist.
- self_
referential_ followers_ and_ following - A self-referential many-to-many relation uses pair hints to distinguish
the two foreign keys on the join model. Each direction then exposes its
own
has_many(via = ...)traversal.