12

I'm loosely following Diesel's getting started guide trying to set up a relational database, but getting the following error on compile:

error[E0433]: failed to resolve: use of undeclared type or module `birds`
 --> src/models.rs:9:12
  |
9 | pub struct Bird {
  |            ^^^^ use of undeclared type or module `birds`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.
error: Could not compile `prrr_gql`.

Here's the binary:

extern crate prrr_gql;
extern crate diesel;

use self::prrr_gql::*;
use self::models::*;
use self::diesel::prelude::*;

fn main() {
    use prrr_gql::schema::cats::dsl::*;
    use prrr_gql::schema::birds::dsl::*;

    let connection = establish_connection();
    let results = cats.load::<Cat>(&connection)
        .expect("Error hearding cats");

    for cat in results {
        println!("{}", cat.name);
    }
}

and lib.rs (imported as prrr_gql)

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

use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;

pub mod schema;
pub mod models;

pub fn establish_connection() -> PgConnection {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL")
        .expect("DATABASE_URL must be set");

    PgConnection::establish(&database_url)
        .expect(&format!("Error connecting to {}", database_url))
}

models.rs

#[derive(Queryable, Debug)]
pub struct Cat {
    pub id: i32,
    pub name: String,
}

#[derive(Queryable, Associations, Debug)]
#[belongs_to(Cat)]
pub struct Bird {
    pub id: i32,
    pub cat_id: i32,
    pub species: String,
    pub colors: String
}

and the schema.rs generated by Diesel

table! {
    birds (id) {
        id -> Int4,
        species -> Varchar,
        colors -> Varchar,
        cat_id -> Nullable<Int4>,
    }
}

table! {
    cats (id) {
        id -> Int4,
        name -> Varchar,
    }
}

joinable!(birds -> cats (cat_id));

allow_tables_to_appear_in_same_query!(
    birds,
    cats,
);

The only issue I could find related to this says I need to have birds in scope and references the table! macro that I have provided, so I'm not sure what's missing.

When I comment out everything related to the birds database, everything runs as expected.

Full project with Cargo.toml for reference: https://github.com/crashspringfield/prrr_gql/tree/diesel-error

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
crash springfield
  • 1,072
  • 7
  • 17
  • 33
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error in a brand new Cargo project then [edit] your question to include the additional info. There are [Rust-](//stackoverflow.com/tags/rust/info) and [Diesel](//stackoverflow.com/tags/diesel/info)-specific tips you can use to reduce your original code for posting here. Thanks! – Shepmaster Jul 02 '19 at 13:03
  • (that last link should be to [the tag page](https://stackoverflow.com/tags/rust-diesel/info), oops!) – Shepmaster Jul 02 '19 at 13:11
  • @Shepmaster I'll review those and updated as needed. I've updated the issue with a github link to a branch that reproduces the error (and I'll keep that up as a future reference) – crash springfield Jul 02 '19 at 13:14
  • [To make Stack Overflow a useful resource for future visitors beyond of the context of your repository](https://meta.stackoverflow.com/q/380194/155423), please [edit] your question to add a [MRE] in the question itself, in addition to the link to your repository. – Shepmaster Jul 02 '19 at 13:15
  • @Shepmaster when I refactor it following Rust's minimal reproducible example, it runs fine. Futhermore, it runs fine if I remove the "belongs_to" macro – crash springfield Jul 02 '19 at 14:01
  • 2
    That's great! That means that you are finding more details about the problem that you can update the question with. For example, you now know that `belongs_to` is responsible, so that can be part of your question title. I've updated that to show an example of how you can improve your question. – Shepmaster Jul 02 '19 at 14:04
  • 1
    Next, take smaller steps to reduce the problem. When the problem goes away, undo that change and try removing / changing other things. It's highly likely that you'll solve your own problem this way. – Shepmaster Jul 02 '19 at 14:06

1 Answers1

15

As the error mentions, birds is not in scope. The table! macro creates a public module (birds), which you then need to bring into scope to be able to derive Associations (in models.rs):

use super::schema::birds;

See diesel::associations for an example, where it shows that one needs to use the schema for derive.

ocstl
  • 386
  • 1
  • 3
  • 10