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>,
}
}