没有为“DbConnection”实现特征“diesel::Connection”

Mic*_*ans 5 postgresql rust rust-diesel rust-rocket

我正在尝试使用柴油将 postgres 数据库添加到火箭应用程序。我的main.rs文件看起来像这样,但给出了错误“该特征diesel::Connection未实现DbConnection.get_result(connection)

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

use diesel::prelude::*;
use rocket_contrib::database;
use rocket_contrib::json::JsonValue;

mod models;
mod schema;

use self::models::*;
use self::schema::*;

#[database("my_db")]
struct DbConnection(diesel::PgConnection);

#[get("/")]
fn index(connection: DbConnection) -> JsonValue {
    json!(all_bicycles(&connection))
}

fn create_bicycle<'a>(connection: &DbConnection, make: &'a str, model: &'a str, rider_type: &'a str, size: &'a str) -> Bicycle {
    let new_bicycle = NewBicycle {
        make,
        model,
        rider_type,
        size
    };

    diesel::insert_into(bicycles::table)
        .values(new_bicycle)
        // the error is on the following line, on `connection`
        .get_result(connection)
        .expect("Error saving bicycle")
}

fn main() {
    rocket::ignite()
        .attach(DbConnection::fairing())
        .mount("/", routes![index])
        .launch();
}

Run Code Online (Sandbox Code Playgroud)

我的Cargo.toml(相关部分)

[dependencies]
diesel = { version = "1.4.4", features = ["postgres"] }
dotenv = "0.15.0"
rocket = { git = "https://github.com/SergioBenitez/Rocket" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[dependencies.rocket_contrib]
git = "https://github.com/SergioBenitez/Rocket"
default-features = false
features = ["json", "diesel_postgres_pool"]

Run Code Online (Sandbox Code Playgroud)

和我的Rocket.toml

[global.databases]
my_db = { url = "postgres://postgres:@localhost/bikes" }

Run Code Online (Sandbox Code Playgroud)

展开时错误如下所示:

&DbConnection
the trait bound `DbConnection: diesel::Connection` is not satisfied

the trait `diesel::Connection` is not implemented for `DbConnection`
Run Code Online (Sandbox Code Playgroud)

我已经成功建立了与数据库的连接diesel setup。我还可以添加迁移 - 尽管我认为它们对于这个问题来说是不必要的。

我在这里做错了什么?

编辑

我再次浏览了 Rocket 文档,意识到我错过了use rocket_contrib::databases::diesel;与 冲突的行extern crate diesel;,所以我将数据库逻辑移到了一个新模块中 - database.rs。没有什么真正改变,但新模块看起来像这样:

[dependencies]
diesel = { version = "1.4.4", features = ["postgres"] }
dotenv = "0.15.0"
rocket = { git = "https://github.com/SergioBenitez/Rocket" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[dependencies.rocket_contrib]
git = "https://github.com/SergioBenitez/Rocket"
default-features = false
features = ["json", "diesel_postgres_pool"]

Run Code Online (Sandbox Code Playgroud)

它的使用方式如下:

main.rs

[global.databases]
my_db = { url = "postgres://postgres:@localhost/bikes" }

Run Code Online (Sandbox Code Playgroud)

错误仍然相同。

wei*_*ich 3

根据Rocket文档,您需要将连接类型取消引用为某种类型,diesel::connection::Connection因为包装器类型没有实现必要的特征。因此,您需要将代码更改为以下内容:

    diesel::insert_into(bicycles::table)
        .values(new_bicycle)
        // the error is on the following line, on `connection`
        .get_result(&*connection)
        .expect("Error saving bicycle")
Run Code Online (Sandbox Code Playgroud)

&*(请注意将连接传递到函数之前的附加内容get_result。)

  • 感谢您的回答,但遗憾的是没有成功。我对 Rust 还很陌生,但我相信问题在于“#[database]”没有正确地将特征应用到结构中,也就是说我不知道​​如何修复它,所以我只是猜测。 (2认同)