lib.rs:
#[macro_use]
extern crate diesel;
extern crate dotenv;
extern crate serde_json;
pub mod schema;
pub mod models;
use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;
pub fn establish_connection() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}
schema.rs:
table! {
photo (id) {
id -> Int8,
datadoc -> Nullable,
}
}
models.rs:
use super::schema::photo;
use serde_json::Value;
#[derive(Queryable)]
pub struct Photo {
pub id: i64,
pub datadoc: String,
}
#[derive(Insertable)]
#[table_name="photo"]
pub struct NewPhoto {
pub id: i64,
pub datadoc: Option,
}
main.rs:
extern crate diesel;
extern crate database;
use database::models::Photo;
use diesel::prelude::*;
fn main() {
use database::schema::photo::dsl::*;
let connection = database::establish_connection();
let results = photo
.load::<Photo>(&connection)
.expect("Error loading photos");
println!("loaded photos");
}
Compiler error message:
Compiling photography v0.1.0 (file:///C:/.../source_code)
error[E0277]: the trait bound *const str: diesel::deserialize::FromSql<diesel::sql_types::Nullable<diesel::sql_types::Jsonb>, _>
is not satisfied
--> src\main.rs:12:10
|
12 | .load::(&connection)
| ^^^^ the trait diesel::deserialize::FromSql<diesel::sql_types::Nullable<diesel::sql_types::Jsonb>, _>
is not implemented for *const str
|
= help: the following implementations were found:
<*const str as diesel::deserialize::FromSql<diesel::sql_types::Text, DB>>
<*const [u8] as diesel::deserialize::FromSql<diesel::sql_types::Binary, DB>>
= note: required because of the requirements on the impl of diesel::deserialize::FromSql<diesel::sql_types::Nullable<diesel::sql_types::Jsonb>, _>
for std::string::String
= note: required because of the requirements on the impl of diesel::deserialize::FromSqlRow<diesel::sql_types::Nullable<diesel::sql_types::Jsonb>, _>
for std::string::String
= note: required because of the requirements on the impl of diesel::Queryable<diesel::sql_types::Nullable<diesel::sql_types::Jsonb>, _>
for std::string::String
= note: required because of the requirements on the impl of diesel::Queryable<(diesel::sql_types::BigInt, diesel::sql_types::Nullable<diesel::sql_types::Jsonb>), _>
for (i64, std::string::String)
= note: required because of the requirements on the impl of diesel::Queryable<(diesel::sql_types::BigInt, diesel::sql_types::Nullable<diesel::sql_types::Jsonb>), _>
for database::models::Photo
= note: required because of the requirements on the impl of diesel::query_dsl::LoadQuery<_, database::models::Photo>
for database::schema::photo::table
error: aborting due to previous error
error: Could not compile photography
.
To learn more, run the command again with --verbose.
Process finished with exit code 101