rent
Learn by example

18. Constraints and cascades

Rent does not replace database integrity. The schema and migrations create real foreign keys, unique constraints, and referential actions.

The runnable chapter attempts to create a comment for a missing post and requires the database to reject it. It then deletes a real post and verifies ON DELETE CASCADE removed the dependent comment.

let orphan = client
    .create_comment()
    .post_id(999)
    .author_id(1)
    .save()
    .await;

assert!(orphan.is_err());

client.delete_post_one(&post).exec().await?;
assert_eq!(client.comment().count().await?, 0);

It also creates a recursive parent/child relationship, deletes the parent, and verifies ON DELETE SET NULL detached the child without deleting it.

The foreign keys and referential actions are all visible in crates/rent/examples/tutorial_17_constraints_and_cascades/schema.rsl. Run cargo run -p rent --example tutorial_17_constraints_and_cascades. The same integrity workflow runs under nextest.