Reputation: 38777
Trying to get past an error in Rust and sqlx (0.7x) using query_as
and fetch_all
functions and a defined struct. Goal is to query from sqlite and deserialize to a vec of struct.
use serde::{Deserialize, Serialize};
use sqlx::sqlite::SqlitePoolOptions;
#[derive(Serialize, Deserialize)]
struct Todo {
id: String,
completed: bool,
}
#[tokio::main]
async fn main() {
// set up connection pool
let pool = SqlitePoolOptions::new()
// .max_connections(5)
// .acquire_timeout(Duration::from_secs(3))
.connect("sqlite:mydatabase.db")
.await
.expect("can't connect to database");
let todos: Vec<Todo> = sqlx::query_as::<_, Todo>("SELECT * FROM todos")
.fetch_all(&pool)
.await
.unwrap();
}
Error is as follows:
the trait bound `for<'r> Todo: FromRow<'r, _>` is not satisfied
the following other types implement trait `FromRow<'r, R>`:
()
(T1,)
(T1, T2)
(T1, T2, T3)
(T1, T2, T3, T4)
(T1, T2, T3, T4, T5)
(T1, T2, T3, T4, T5, T6)
(T1, T2, T3, T4, T5, T6, T7)
and 9 others
the method `fetch_all` exists for struct `QueryAs<'_, _, Todo, _>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`Todo: FromRow<'r, _>`
Upvotes: 1
Views: 694
Reputation: 38777
Adding FromRow
resolved this issue:
use sqlx::{FromRow};
#[derive(Serialize, Deserialize, FromRow)]
struct Todo {
id: String,
completed: bool,
}
Upvotes: 1