toasty_driver_integration_suite/tests/
belongs_to_one_way.rs

1use crate::prelude::*;
2
3#[driver_test(id(ID))]
4pub async fn crud_user_optional_profile_one_direction(test: &mut Test) -> Result<()> {
5    #[derive(Debug, toasty::Model)]
6    struct User {
7        #[key]
8        #[auto]
9        id: ID,
10
11        #[index]
12        profile_id: Option<ID>,
13
14        #[belongs_to(key = profile_id, references = id)]
15        profile: toasty::BelongsTo<Option<Profile>>,
16    }
17
18    #[derive(Debug, toasty::Model)]
19    struct Profile {
20        #[key]
21        #[auto]
22        id: ID,
23    }
24
25    let mut db = test.setup_db(models!(User, Profile)).await;
26
27    // Create a user
28    let user = User::create()
29        .profile(Profile::create())
30        .exec(&mut db)
31        .await?;
32
33    assert!(user.profile_id.is_some());
34    Ok(())
35}