3

I'm building a web service that uses Diesel to access a MySQL database. Everything is setup correctly and Diesel is generating the schema.rs file with content that reflects my database schema:

table! {
    user (id) {
        // ...
    }
}

I created a store.rs file that resides next to main.rs. If my understanding of modules is correct, any code I put in the store.rs file, will belong to a module named store that is a child of the crate module. My intention is to put all the code that deals with database stuff in the store module. However, I can't seem to be able to use the stuff from the schema module in my store module to start doing some querying using the Diesel APIs.

I tried:

  • use schema;
  • use crate::schema;
  • use super::schema;
  • use super::schema::user;

Nothing works. The compiler always says that it cannot resolve one piece of the path or another.

What is the proper way to reference a sibling module in Rust?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
AxiomaticNexus
  • 6,190
  • 3
  • 41
  • 61

2 Answers2

1

In your main.rs, make sure you're setting diesel for #[macro_use] and importing the schema mod.

#[macro_use]
extern crate diesel;
mod schema;

In store.rs, you should be able to use the schema as you see fit.

use crate::schema::user;
JesseM93
  • 11
  • 1
0

I hope this would help.

in store.rs

use crate::schema::*;

// … any other diesel related code you want to put here

in main.rs

pub mod schema;
mod store;
weiznich
  • 2,910
  • 9
  • 16