ORM-22: Add attach, detach and sync for many-to-many #22

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

Depends on: #8, #21

Context

Writes to a many-to-many relation only touch the pivot table. They are idempotent (the pivot has a composite primary key) and sync is atomic.

Tasks

  • attach(&b) and attach_many(&[&B]) use INSERT OR IGNORE INTO <pivot> so repeating is idempotent.
  • detach(&b) and detach_all().
  • sync(&[i64]) diffs against the current pivot rows and applies the inserts and deletes inside one Database::transaction.
  • Both sides must be persisted, otherwise Error::NotPersisted.

Example

let post = Post::find(1)?.unwrap();
let (rust, web, orm) = (Tag::find(1)?.unwrap(), Tag::find(2)?.unwrap(), Tag::find(3)?.unwrap());

post.tags().attach(&rust)?;          // INSERT OR IGNORE INTO post_tags (post_id, tag_id) VALUES (?, ?)
post.tags().attach_many(&[&web, &orm])?;
post.tags().detach(&web)?;

post.tags().sync(&[rust.id, orm.id])?; // pivot now holds exactly these two
post.tags().detach_all()?;
#[test]
fn attaching_twice_is_idempotent() {
    setup_with_relations();
    let post = Post::find(1).unwrap().unwrap();
    let tag = Tag::find(1).unwrap().unwrap();
    post.tags().attach(&tag).unwrap();
    post.tags().attach(&tag).unwrap();
    assert_eq!(post.tags().all().unwrap().len(), 1);
}

Acceptance criteria

  • Attaching twice leaves one pivot row; detach removes it.
  • sync adds and removes the right ids and leaves unchanged ones alone.
  • A failure partway through sync leaves the pivot unchanged.
  • Unsaved models return NotPersisted.
**Depends on:** #8, #21 ## Context Writes to a many-to-many relation only touch the pivot table. They are idempotent (the pivot has a composite primary key) and `sync` is atomic. ## Tasks - [ ] `attach(&b)` and `attach_many(&[&B])` use `INSERT OR IGNORE INTO <pivot>` so repeating is idempotent. - [ ] `detach(&b)` and `detach_all()`. - [ ] `sync(&[i64])` diffs against the current pivot rows and applies the inserts and deletes inside one `Database::transaction`. - [ ] Both sides must be persisted, otherwise `Error::NotPersisted`. ## Example ```rust let post = Post::find(1)?.unwrap(); let (rust, web, orm) = (Tag::find(1)?.unwrap(), Tag::find(2)?.unwrap(), Tag::find(3)?.unwrap()); post.tags().attach(&rust)?; // INSERT OR IGNORE INTO post_tags (post_id, tag_id) VALUES (?, ?) post.tags().attach_many(&[&web, &orm])?; post.tags().detach(&web)?; post.tags().sync(&[rust.id, orm.id])?; // pivot now holds exactly these two post.tags().detach_all()?; ``` ```rust #[test] fn attaching_twice_is_idempotent() { setup_with_relations(); let post = Post::find(1).unwrap().unwrap(); let tag = Tag::find(1).unwrap().unwrap(); post.tags().attach(&tag).unwrap(); post.tags().attach(&tag).unwrap(); assert_eq!(post.tags().all().unwrap().len(), 1); } ``` ## Acceptance criteria - [ ] Attaching twice leaves one pivot row; `detach` removes it. - [ ] `sync` adds and removes the right ids and leaves unchanged ones alone. - [ ] A failure partway through `sync` leaves the pivot unchanged. - [ ] Unsaved models return `NotPersisted`.
Sign in to join this conversation.
No description provided.