本文介绍了“使用未声明的类型或模块"使用 Diesel 的 `belongs_to` 属性时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大致遵循 Diesel 的入门指南,尝试设置关系数据库,但在编译时出现以下错误:

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`.

这是二进制文件:

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);
    }
}

和 lib.rs(导入为 prrr_gql)

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
}

和 Diesel 生成的 schema.rs

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,
);

我能找到的唯一一个问题说我需要有birds 并引用了我提供的 table! 宏,所以我不确定缺少什么.

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.

当我注释掉与 birds 数据库相关的所有内容时,一切都按预期运行.

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

带有 Cargo.toml 的完整项目供参考:https://github.com/crashspringfield/prrr_gql/tree/diesel-error

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

推荐答案

如错误所述,birds 不在范围内.table! 宏创建了一个公共模块 (birds),然后您需要将其引入范围才能派生 Associations(在模型中.rs):

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;

参见 diesel::associations 的示例,其中显示一个需要use derive 的架构.

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

这篇关于“使用未声明的类型或模块"使用 Diesel 的 `belongs_to` 属性时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 22:54