【发布时间】:2021-04-28 09:32:42
【问题描述】:
我有三个具有这些关系的模型:
艺术家 1n 歌曲 1n 播放
我想加载 Plays 并急切地加载它的歌曲和艺术家。
我在 PHP 框架 Eloquent 中轻松做到了这一点。而且它只需要 3 个数据库请求(每个模型一个)。
这在柴油上怎么可能?
我认为 Play 模型应该如下所示:
#[derive(Queryable, Serialize, Deserialize)]
pub struct Play {
pub id: u64,
pub song_id: u64,
pub song: Song, // <-- ????
pub date: NaiveDateTime,
pub station_id: u64,
}
并加载类似这样的内容:
// loads one model only at the moment
// it should load the related models Song and Artist too
let items = plays.filter(date.between(date_from, date_to)).load(&*conn)?;
生成的结构应为 JSON 序列化以供 REST API 使用。但是我找不到以一种简单有效的方式获取所需结构的方法。
【问题讨论】:
标签: rust orm eager-loading rust-diesel