0

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 ?
Here is the import error

pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
Glenn singh
  • 233
  • 1
  • 6
  • 18
  • Does this answer your question? [How do I "use" or import a local Rust file?](https://stackoverflow.com/questions/45519176/how-do-i-use-or-import-a-local-rust-file) or perhaps [Rust package with both a library and a binary?](https://stackoverflow.com/questions/26946646/rust-package-with-both-a-library-and-a-binary) – kmdreko Nov 21 '20 at 19:20
  • @kmdrekoIt does helpful but when I added "mod models;" and "mod schema;" in main.rs then it's giving me this error "couldn't read src\models.rs: stream did not contain valid UTF-8". As per the link which you have provided, my scenario is about "Including" internal code. Why I am still getting error !!! – Glenn singh Nov 22 '20 at 05:43
  • That sounds to me like the next error. Might your files not be UTF-8 encoded? (tbh I'm not even sure if that's required, but that'd be the next thing I'd look at). – rethab Nov 22 '20 at 08:41
  • The above link given by @kmdreko has worked for me. It was a problem with vs code rust extension. I upgraded that and error went. Thank you everyone!! – Glenn singh Nov 22 '20 at 14:12

0 Answers0