Repository navigation
|
I am using loco playground and trying to retrieve ranked user balances using Rust Code: #[allow(unused_imports)]
use loco_rs::{cli::playground, prelude::*};
use myapp::app::App;
use myapp::models::_entities::wallets::{self, ActiveModel, Entity, Model};
use sea_orm::{DatabaseConnection, DbBackend, Statement, Value};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Debug)]
struct UserRank {
name: String,
rank: i64,
balance: i32,
}
#[tokio::main]
async fn main() -> loco_rs::Result<()> {
let _ctx = playground::<App>().await?;
let db = &_ctx.db;
let coins_query = r#"
SELECT t.first_name, t.last_name, w.coins,
RANK() OVER (ORDER BY w.coins DESC) AS `rank`
FROM wallets w
JOIN game_users g ON w.game_user_id = g.id
JOIN telegram_users t ON g.id = t.game_user_id
ORDER BY w.coins DESC LIMIT 10;
"#;
let coins = db
.query_all(Statement::from_string(DbBackend::MySql, coins_query))
.await
.unwrap()
.into_iter()
.map(|row| UserRank {
name: format!(
"{} {}",
row.try_get::<String>("", "first_name").unwrap_or_default(),
row.try_get::<String>("", "last_name").unwrap_or_default()
),
rank: row.try_get::<i64>("", "rank").unwrap_or(0), // Always returns 0
balance: row.try_get::<i32>("", "coins").unwrap_or(0),
})
.collect::<Vec<_>>();
println!("User Ranks: {:#?}", coins);
Ok(())
}Output (Unexpected Behavior): Things I Have Tried:
Why is |
Answered by
alokmahor
Mar 21, 2025
Replies: 1 comment
|
The correct type for RANK() OVER (...) in MySQL when using SeaORM is u32 or u64. Changing the rank field to u32 or u64 resolves the issue and retrieves the correct values. |
0 replies
Answer selected by
alokmahor
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The correct type for RANK() OVER (...) in MySQL when using SeaORM is u32 or u64. Changing the rank field to u32 or u64 resolves the issue and retrieves the correct values.