Skip to main content

toasty_driver_integration_suite/tests/
relation_belongs_to_null_fk.rs

1use crate::prelude::*;
2
3#[driver_test(id(ID))]
4pub async fn optional_belongs_to_null_fk(test: &mut Test) -> Result<()> {
5    #[derive(Debug, toasty::Model)]
6    struct User {
7        #[key]
8        #[auto]
9        id: ID,
10    }
11
12    #[derive(Debug, toasty::Model)]
13    struct Post {
14        #[key]
15        #[auto]
16        id: ID,
17
18        #[index]
19        user_id: Option<ID>,
20
21        #[belongs_to(key = user_id, references = id)]
22        user: toasty::Deferred<Option<User>>,
23    }
24
25    let mut db = test.setup_db(models!(User, Post)).await;
26
27    let orphan = toasty::create!(Post {}).exec(&mut db).await?;
28    assert_eq!(orphan.user_id, None);
29
30    // Should return `None` rather than panic in the planner.
31    assert_none!(orphan.user().exec(&mut db).await?);
32
33    Ok(())
34}