Michael
Michael

Reputation: 932

How can I resolve "use of undeclared crate or module" error when using #[diesel(table_name = ...)]?

I have a Rust project using Rocket and Diesel 1.4. I am using MariaDB as my database. My model and schema is generated this way:

diesel print-schema --database-url mysql://root:[email protected]: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.

//! 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.

Upvotes: 4

Views: 2611

Answers (1)

weiznich
weiznich

Reputation: 3455

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)

Upvotes: 3

Related Questions