Reputation: 38985
I am learning use rust diesel to select rows from PostgreSQL 13, here is my code:
#[get("/v1/detail/<songId>")]
pub fn detail(songId: &str) -> io::Result<String> {
use schema::songs::dsl::*;
let connection = config::establish_connection();
let results = songs.filter(source_id.eq(songId))
.limit(5)
.load::<Songs>(&connection)
.expect("Error loading posts");
let result_value = serde_json::to_string(&results).unwrap();
let res = ApiResponse {
body: result_value,
..Default::default()
};
let response_json = serde_json::to_string(&res).unwrap();
return Ok(response_json);
}
when I build this code, shows error like this:
error[E0277]: the trait bound `&i64: Queryable<BigInt, _>` is not satisfied
--> src/biz/music/songs.rs:23:10
|
23 | .load::<Songs>(&connection)
| ^^^^ the trait `Queryable<BigInt, _>` is not implemented for `&i64`
the Songs
define like this:
#[derive(Insertable,Serialize,Queryable)]
#[table_name="songs"]
pub struct Songs<'a> {
pub id: &'a i64,
pub name: &'a str,
pub artists: &'a str,
pub album_id: &'a i64,
pub publishtime: &'a i64,
pub status: &'a i32,
pub duration: &'a i32,
pub source_id: &'a str,
pub source: &'a i32,
pub created_time: &'a i64,
pub updated_time: &'a i64
}
this is my database dml:
CREATE TABLE public.songs (
id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
"name" varchar NOT NULL,
artists varchar NOT NULL,
album_id int8 NOT NULL,
publishtime int8 NULL,
status int4 NOT NULL DEFAULT 1,
duration int4 NULL,
source_id varchar NOT NULL,
"source" int4 NOT NULL,
created_time int8 NULL,
updated_time int8 NULL,
CONSTRAINT songs_id_seq_pk PRIMARY KEY (id),
CONSTRAINT unique_songs UNIQUE (name, artists, album_id),
CONSTRAINT unique_songs_source UNIQUE (source, source_id)
);
what should I do to fix this problem?
Upvotes: 0
Views: 1855
Reputation: 1245
Queryable is expecting a tuple that looks like (i64, str, str, ...) no (&i64, &str, ..), see https://github.com/diesel-rs/diesel/blob/master/guide_drafts/trait_derives.md.
You have to define Songs like this
#[derive(Serialize,Queryable)]
#[table_name="songs"]
pub struct Songs {
pub id: i64,
pub name: str,
pub artists: str,
pub album_id: i64,
pub publishtime: i64,
pub status: i32,
pub duration: i32,
pub source_id: str,
pub source: i32,
pub created_time: i64,
pub updated_time: i64
}
Upvotes: 1