Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I resolve this Diesel model compilation error

I have a rust project consisting of rocket and diesel. I am using mariadb as my database.

My model and schema is generated this way:

diesel print-schema --database-url mysql://root:password@127.0.0.1:3306/mydb > src/schema.rs
diesel_ext -I "diesel::sql_types::*" -I "crate::schema::*" -d "Insertable, Queryable, Debug" -s .\src\schema.rs -m > .\src\model.rs

From what I can tell those tools reads Rocket.toml and generates the code from there.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

schema.rs:

table! {
    auth_guest (id) {
        id -> Unsigned<Integer>,
        device_id -> Varchar,
    }
}

model.rs

// Generated by diesel_ext

#![allow(unused)]
#![allow(clippy::all)]

use diesel::sql_types::*;
use crate::schema::auth_guest;

#[derive(Insertable, Queryable, Debug)]
#[diesel(table_name = auth_guest)]
pub struct AuthGuest {
    pub id: u32,
    pub device_id: String,
}

main.rs:

#![feature(decl_macro)]
#[macro_use] extern crate diesel;
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;

mod schema;
mod model;

use std::borrow::BorrowMut;
use diesel::prelude::*;
use crate::schema::auth_guest::*;

#[database("mydb")]
struct Db(diesel::MysqlConnection);


#[get("/")]
fn read(conn: Db) -> String {
    let guest_user = model::AuthGuest {
        id: 0,
        device_id: String::from("Hello")
    };

    diesel::insert_into(schema::auth_guests::table)
        .values(guest_user)
        .execute(&conn)
        .expect("Error creating user");
}

fn main() {
    rocket::ignite()
        .attach(Db::fairing())
        .launch();
}

The error I am getting is:

error[E0433]: failed to resolve: use of undeclared crate or module `auth_guests`
  --> src\model.rs:11:12
   |
11 | pub struct AuthGuest {
   |            ^^^^^^^^^ use of undeclared crate or module `auth_guests`

I cant seem to figure out what I am doing wrong or missing regarding the compilation error.

>Solution :

The code generated by diesel_ext does not match the code your diesel version is expecting. The #[diesel(table_name = …)] attribute is only available for the upcoming 2.0 release. Based on your usage of rocket and the error message I would assume that you use diesel 1.4, which does not use this attribute. You need to use #[table_name = "…"] there instead or update to a newer diesel version (there are release candidates on crates.io, but I’m not sure if that’s supported by rocket yet)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading