How to write a good select sub query in SeaORM?

Viewed 31
select wallets.*, users.name as name, 
    (select max(regist_at) from payments 
        where payment_item_id = 2 and receiver_id = 1) as newest_login_at
from wallets 
inner join users on wallets.user_id = users.user_id 
where wallets.user_id = 1;

I would like to execute the above sql statement in sea-orm, but I don't know how to do it.
If possible, I would like to write the following natural code, but this is a compile error.
Does anyone know a better way?

rustc 1.65.0-nightly | sea-orm "0.9.2"

#[derive(Debug, FromQueryResult, Serialize, Deserialize)]
pub struct WalletSummary {
    pub user_id: i64,
    pub name: String,
    pub amount: i64,
    pub regist_at: DateTimeWithTimeZone,
    pub newest_login_at: Option<DateTimeWithTimeZone>,
}

let user_id: i64 = 2;
let wallet_summary = Wallets::find_by_id(user_id)
    .column(users::Column::Name)
    .column_as(
        Payments::find()
            .column(payments::Column::RegistAt.max())
            .filter(payments::Column::PaymentItemId.eq(1))
            .filter(payments::Column::ReceiverId.eq(user_id)),
        "newest_login_at"
   )
    .join(JoinType::InnerJoin, wallets::Relation::Users.def())
    .into_model::<WalletSummary>()
    .one(db)
    .await?
0 Answers
Related