ORM-20: Implement BelongsTo #20

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

Depends on: #18

Context

The inverse of HasMany / HasOne: the model holding the foreign key looks up its parent (post.author()). associate sets the FK field without saving, so the child becomes dirty and save() persists it, like Eloquent.

Tasks

  • BelongsTo::<C, P>::new(fk: Column<C, i64>, get_fk: fn(&C) -> Option<i64>, set_fk: fn(&mut C, Option<i64>)), plus a const .nullable() marker.
  • .of(&child) returns a handle with first() -> Result<Option<P>>. A NULL FK returns None without querying.
  • associate(&mut child, &parent) -> Result<()> sets the FK (the parent must be persisted, otherwise Error::NotPersisted). dissociate(&mut child) sets it to NULL and returns the new Error::NotNullable unless the relation was marked .nullable().
  • Sugar: post.author().

Example

impl Post {
    pub const AUTHOR: BelongsTo<Post, User> = BelongsTo::new(
        Post::USER_ID,
        |p| Some(p.user_id),
        |p, id| p.user_id = id.unwrap_or_default(),
    );

    pub fn author(&self) -> BelongsToOf<User> { Self::AUTHOR.of(self) }
}
let post = Post::find(1)?.unwrap();
let author: Option<User> = post.author().first()?;

// associate + save, like Eloquent
let alice = User::find(1)?.unwrap();
let mut draft = Post::new(0, "Draft", false);
Post::AUTHOR.associate(&mut draft, &alice)?;  // draft.user_id = alice.id; not saved yet
assert!(draft.is_dirty());
draft.save()?;

// `user_id` is NOT NULL here, so dissociating is refused
assert!(matches!(Post::AUTHOR.dissociate(&mut draft), Err(Error::NotNullable)));

Acceptance criteria

  • The lookup returns the parent.
  • A NULL FK returns None and runs no query.
  • associate makes the child dirty on its FK column and save() persists it.
  • dissociate on a non-nullable relation returns NotNullable.
**Depends on:** #18 ## Context The inverse of `HasMany` / `HasOne`: the model holding the foreign key looks up its parent (`post.author()`). `associate` sets the FK field without saving, so the child becomes dirty and `save()` persists it, like Eloquent. ## Tasks - [ ] `BelongsTo::<C, P>::new(fk: Column<C, i64>, get_fk: fn(&C) -> Option<i64>, set_fk: fn(&mut C, Option<i64>))`, plus a const `.nullable()` marker. - [ ] `.of(&child)` returns a handle with `first() -> Result<Option<P>>`. A NULL FK returns `None` without querying. - [ ] `associate(&mut child, &parent) -> Result<()>` sets the FK (the parent must be persisted, otherwise `Error::NotPersisted`). `dissociate(&mut child)` sets it to NULL and returns the new `Error::NotNullable` unless the relation was marked `.nullable()`. - [ ] Sugar: `post.author()`. ## Example ```rust impl Post { pub const AUTHOR: BelongsTo<Post, User> = BelongsTo::new( Post::USER_ID, |p| Some(p.user_id), |p, id| p.user_id = id.unwrap_or_default(), ); pub fn author(&self) -> BelongsToOf<User> { Self::AUTHOR.of(self) } } ``` ```rust let post = Post::find(1)?.unwrap(); let author: Option<User> = post.author().first()?; // associate + save, like Eloquent let alice = User::find(1)?.unwrap(); let mut draft = Post::new(0, "Draft", false); Post::AUTHOR.associate(&mut draft, &alice)?; // draft.user_id = alice.id; not saved yet assert!(draft.is_dirty()); draft.save()?; // `user_id` is NOT NULL here, so dissociating is refused assert!(matches!(Post::AUTHOR.dissociate(&mut draft), Err(Error::NotNullable))); ``` ## Acceptance criteria - [ ] The lookup returns the parent. - [ ] A NULL FK returns `None` and runs no query. - [ ] `associate` makes the child dirty on its FK column and `save()` persists it. - [ ] `dissociate` on a non-nullable relation returns `NotNullable`.
Sign in to join this conversation.
No description provided.