Diesel - cannot find derive macro `InferSchema`


#1

Hello, I’m trying to use Diesel in a Rocket project. The problem is that I am not able to even run the getting started example (adapted to work on a Rocket app). I have put all the database logic in a db module, having two submodules, schema and models.

In the index function, I have basically copied the structure in the getting started guide (only by changing the name of the Post struct for Component. The problem is that I get this:

error[E0432]: unresolved import `db::schema::components::dsl::*`
  --> src/main.rs:30:9
   |
30 |     use db::schema::components::dsl::*;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Could not find `components` in `schema`

error: cannot find derive macro `InferSchema` in this scope
 --> <infer_schema macros>:3:14
  |
3 | # [ derive ( InferSchema ) ] # [
  |              ^^^^^^^^^^^

error[E0425]: cannot find value `components` in this scope
  --> src/main.rs:34:19
   |
34 |     let results = components.filter(id.eq(1))
   |                   ^^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `id` in this scope
  --> src/main.rs:34:37
   |
34 |     let results = components.filter(id.eq(1))
   |                                     ^^ not found in this scope

error: aborting due to 4 previous errors

So it seems it cannot find the InferSchema codegen? I am importing both Diesel and diesel_codegen:

#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_codegen;

Is there any limitation where I cannot have the database logic in a submodule?

This is my index function:

fn index() -> Template {
    use db::schema::components::dsl::*;
    use db::models::Component;

    let connection = establish_connection();
    let results = components.filter(id.eq(1))
        .limit(1)
        .load::<Component>(&connection)
        .expect("Error loading components");

    let mut context = HashMap::new();
    Template::render("index", &context)
}

diesel print-schema shows this:

table! {
    components (id) {
        id -> Mediumint,
        name -> Varchar,
        price -> Decimal,
        purchase_date -> Nullable<Timestamp>,
    }
}

#2

Do you have features = ["postgres"] (or whatever backend you’re using) on the diesel_codegen requirement?


#3

Yes, I’m using MySQL, these are my dependencies:

[dependencies]
rocket = "0.2.3"
rocket_codegen = "0.2.3"
diesel_codegen = "0.12"
dotenv = "0.10"
chrono = "0.3"

[dependencies.diesel]
version = "0.12"
default-features = false
features = ["mysql", "chrono"]

I removed default features, but I have mysql as a feature.


#4

I found the error! I wasn’t including the “mysql” feature in diesel_codegen. But I now have two new errors. The table I want to represent is this:

CREATE TABLE `components` (
  `id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL,
  `price` DECIMAL(6,2) UNSIGNED NOT NULL,
  `purchase_date` TIMESTAMP NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And Diesel tells me that Mediumint and Decimal were not found:

error[E0412]: cannot find type `Mediumint` in this scope
 --> src/db/schema.rs:3:1
  |
3 | infer_schema!("dotenv:DATABASE_URL");
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
  |
  = note: this error originates in a macro outside of the current crate

error[E0412]: cannot find type `Decimal` in this scope
 --> src/db/schema.rs:3:1
  |
3 | infer_schema!("dotenv:DATABASE_URL");
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
  |
  = note: this error originates in a macro outside of the current crate

What can I do?