ORM-21: Implement BelongsToMany (read side) #21

Open
opened 2026-10-13 12:49:40 +01:00 by NoctisWolfshadow · 0 comments
Owner

Depends on: #18

Context

Many-to-many goes through a pivot table. To avoid joins and a pivot Model, the relation is expressed as an IN (subquery) filter on the other model, built with the already-public Expression::new. Both sides declare the relation, with the key names swapped.

Tasks

  • BelongsToMany::<A, B>::new(pivot, local_key, foreign_key), const-constructible.
  • .of(&a) returns a handle whose query() -> Query<B> carries <B pk> IN (SELECT <foreign_key> FROM <pivot> WHERE <local_key> = ?). Same delegating methods as #19.
  • Sugar: post.tags() and tag.posts().

Example

impl Post {
    pub const TAGS: BelongsToMany<Post, Tag> = BelongsToMany::new("post_tags", "post_id", "tag_id");
    pub fn tags(&self) -> BelongsToManyOf<Tag> { Self::TAGS.of(self) }
}

impl Tag {
    pub const POSTS: BelongsToMany<Tag, Post> = BelongsToMany::new("post_tags", "tag_id", "post_id");
    pub fn posts(&self) -> BelongsToManyOf<Post> { Self::POSTS.of(self) }
}
let post = Post::find(1)?.unwrap();
let tags = post.tags().filter(Tag::NAME.like("r%")).order_by(Tag::NAME.asc()).all()?;

SQL for the call above:

SELECT id, name FROM tags
WHERE id IN (SELECT tag_id FROM post_tags WHERE post_id = ?) AND name LIKE ?
ORDER BY name ASC
// the subquery filter, built from pieces that already exist
Expression::<Tag>::new(
    format!("{} IN (SELECT {} FROM {} WHERE {} = ?)", Tag::PRIMARY_KEY, foreign_key, pivot, local_key),
    vec![Value::Integer(post.id())],
)

Acceptance criteria

  • Both directions (post.tags() and tag.posts()) return the right rows.
  • Composes with filter, order_by and limit.
  • A model with no pivot rows returns an empty Vec.
**Depends on:** #18 ## Context Many-to-many goes through a pivot table. To avoid joins and a pivot `Model`, the relation is expressed as an `IN (subquery)` filter on the other model, built with the already-public `Expression::new`. Both sides declare the relation, with the key names swapped. ## Tasks - [ ] `BelongsToMany::<A, B>::new(pivot, local_key, foreign_key)`, const-constructible. - [ ] `.of(&a)` returns a handle whose `query() -> Query<B>` carries `<B pk> IN (SELECT <foreign_key> FROM <pivot> WHERE <local_key> = ?)`. Same delegating methods as #19. - [ ] Sugar: `post.tags()` and `tag.posts()`. ## Example ```rust impl Post { pub const TAGS: BelongsToMany<Post, Tag> = BelongsToMany::new("post_tags", "post_id", "tag_id"); pub fn tags(&self) -> BelongsToManyOf<Tag> { Self::TAGS.of(self) } } impl Tag { pub const POSTS: BelongsToMany<Tag, Post> = BelongsToMany::new("post_tags", "tag_id", "post_id"); pub fn posts(&self) -> BelongsToManyOf<Post> { Self::POSTS.of(self) } } ``` ```rust let post = Post::find(1)?.unwrap(); let tags = post.tags().filter(Tag::NAME.like("r%")).order_by(Tag::NAME.asc()).all()?; ``` SQL for the call above: ```sql SELECT id, name FROM tags WHERE id IN (SELECT tag_id FROM post_tags WHERE post_id = ?) AND name LIKE ? ORDER BY name ASC ``` ```rust // the subquery filter, built from pieces that already exist Expression::<Tag>::new( format!("{} IN (SELECT {} FROM {} WHERE {} = ?)", Tag::PRIMARY_KEY, foreign_key, pivot, local_key), vec![Value::Integer(post.id())], ) ``` ## Acceptance criteria - [ ] Both directions (`post.tags()` and `tag.posts()`) return the right rows. - [ ] Composes with `filter`, `order_by` and `limit`. - [ ] A model with no pivot rows returns an empty `Vec`.
Sign in to join this conversation.
No description provided.