Search code examples
rustrust-diesel

How to return a single owned record with Diesel?


Actually I'm returing a cloned struct

pub fn matrix_first_or_create(&self, schema: String, symbol: String) -> RewardMatrix {
    ....

    let rewards = reward_matrix
        .filter(state.eq(schema.clone()))
        .filter(turn_symbol.eq(symbol.clone()))
        .limit(1)
        .load::<RewardMatrix>(&self.connection)
        .expect("Error loading posts");

    if rewards.len() > 0 {
        return rewards.get(0).unwrap().clone();
    }

    ....
}

Is this the right way to handle a result in Diesel? Can I "extract" and own the first and only result from this query?


Solution

  • What you need here is the .first() method of the RunQueryDsl trait.

    Here is an example of how it works:

    let reward: RewardMatrix = reward_matrix
        .filter(state.eq(schema.clone()))
        .filter(turn_symbol.eq(symbol.clone()))
        .first(&self.connection)?;
    

    But if your query may not return that row, it’s also good to use the .optional() method of QueryResult:

    let reward: Option<RewardMatrix> = reward_matrix
        .filter(state.eq(schema.clone()))
        .filter(turn_symbol.eq(symbol.clone()))
        .first(&self.connection)
        .optional()?;