I'm trying to create a small demo app using the diesel crate. I tried the things below by looking at the diesel getting started guide.
I don't understand how to import my models and schema and get my query working in main. Attached the snapshot shows project structure with that import error.
Here are my files:
schema.rs
table! {
department (dept_id) {
dept_id -> Int4,
dept_name -> Nullable<Text>,
created_on -> Nullable<Timestamp>,
created_by -> Nullable<Text>,
modified_on -> Nullable<Timestamp>,
modified_by -> Nullable<Text>,
is_active -> Nullable<Bool>,
}
}
models.rs
#![allow(unused)]
#![allow(clippy::all)]
use chrono::NaiveDateTime;
use bigdecimal::BigDecimal;
#[derive(Queryable, Debug, Identifiable)]
#[primary_key(dept_id)]
pub struct Department {
pub dept_id: i32,
pub dept_name: Option<String>,
pub created_on: Option<NaiveDateTime>,
pub created_by: Option<String>,
pub modified_on: Option<NaiveDateTime>,
pub modified_by: Option<String>,
pub is_active: Option<bool>,
}
lib.rs
#[macro_use]
extern crate diesel;
extern crate dotenv;
use diesel::prelude::*;
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)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
}
cargo.toml
[dependencies]
diesel = { version = "1.4.5", features = ["postgres"] }
dotenv = "0.15.0"
chrono = { version = "0.4.19" , features = ["serde"] }
bigdecimal = { version = "0.2.0" }
main.rs
use diesel_app::establish_connection;
fn main() {
use diesel_app::schema::department::dsl::*;
let con = establish_connection();
let result = department
.limit(2)
.load::<Department>(&con)
.expect("Error loading department");
}
I tried looking at the resources given by rust but did not understand much.
Can anybody tell me how to resolve these imports and get my query working ?
Where am I going wrong ?