tokio-rs / toasty

An async ORM for Rust (incubating)
MIT License
589 stars 18 forks source link

BelongsTo is None #6

Closed codegod100 closed 1 day ago

codegod100 commented 2 days ago

I'm still grokking the source code but one of my models doesn't have a value on BelongsTo which is throwing a panic

    #[track_caller]
    pub fn get(&self) -> &T {
        self.value.as_ref().expect("association not loaded")
    }

I currently have a model that belongs to more than one model so that might be causing it but I'm not sure. Including my schema file:

model User { 
    #[key]
    #[auto]
    id: Id,

    name: String,
    bookmarks: [Bookmark],
    likes: [Like],
}

model Bookmark {
    #[key]
    #[auto]
    id: Id,

    title: String,
    url: String,
    likes: [Like],

    #[index]
    user_id: Id<User>,
    #[relation(key = user_id, references = id)]
    user: User,
}

model Like {
    #[key]
    #[auto]
    id: Id,

    #[index]
    user_id: Id<User>,
    #[relation(key = user_id, references = id)]
    user: User,

    #[index]
    bookmark_id: Id<Bookmark>,
    #[relation(key = bookmark_id, references = id)]
    bookmark: Bookmark,

}
codegod100 commented 2 days ago

fixed this one by calling find(&db) after user(), I guess that's an api requirement.

Still learning what's a bug and what is user error.

carllerche commented 1 day ago

Yes, it is an API requirement. You can specify includes: https://github.com/tokio-rs/toasty/blob/main/tests/client/tests/has_many_preload.rs#L42-L46

I need to look if I did it yet, but the intent is to include a .query method or something on the association to do an explicit query to load the data.