我松散地遵循 Diesel 的入门指南尝试设置关系数据库,但在编译时出现以下错误:
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
)
#[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))
}
模型.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
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!
我提供的宏,所以我不确定缺少什么。
当我注释掉与birds
数据库相关的所有内容时,一切都按预期运行。
带有 Cargo.toml 的完整项目供参考:https ://github.com/crashspringfield/prrr_gql/tree/diesel-error