我对Rust 还是个新手,我真的很喜欢玩它.然而,我被困在一个错误,因为我的CRUD应用程序使用柴油和火箭.我有一个main.ars、mod.rs和schema.rs.

我在使用我创建的用户 struct 的POST方法中遇到错误.

我正在使用我在后台运行的Postgres数据库,用于路由,在 docker 、柴油和火箭上运行.

我的模特.RS

use super::schema::users;
use diesel::{prelude::*, table, Queryable, Insertable, RunQueryDsl};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Queryable, Debug, Insertable)]
#[table_name= "users"]
pub struct User {
    pub id: i32,
    pub first_name: String,
    pub last_name: String,
    pub user_name: String,
    pub email_address: String,
}

My main.ars(包含了所有细节,但真正的问题是关于Post方法--create_user

#[macro_use] extern crate rocket;

mod models;
mod schema;

use rocket_sync_db_pools::{database};
use models::{User};
use rocket::{serde::json::Json};
use diesel::{RunQueryDsl};
use schema::users;

#[database("my_db")]
pub struct Db(rocket_sync_db_pools::diesel::PgConnection);

#[get("/")]
fn index() -> &'static str {
    "Hello World"
}

#[get("/<id>")]
fn get_user(id: i32) -> Json<User> {
    Json(User {
        id: id,
        first_name: "A Fist Name".to_string(),
        last_name: "A Last Name".to_string(),
        user_name: "A User Name".to_string(),
        email_address: "AnEmail@email.com".to_string(),
    })
}

#[post("/", data = "<user>")]
async fn create_user(connection: Db, user: Json<User>) -> Json<User> {
    connection.run(move |c| {
    diesel::insert_into(users::table)
    .values(&user.into_inner())
    .get_result(c)
    })
    .await
    .map(Json)
    .expect("There was an error saving the user")
}

#[launch]
fn rocket() -> _ {
    let rocket = rocket::build();

    rocket
    .attach(Db::fairing())
    .mount("/", routes![index])
    .mount("/users", routes![get_user, create_user])

}

来自Cargo.toml的依赖项

[dependencies]
diesel = "2.0.2"
diesel_cli = { version = "1.4.1", default-features = false, features = ["postgres"] }
rocket = { version = "0.5.0-rc.2", features = ["json"] }
rocket_sync_db_pools = { version = "0.1.0-rc.2", features = ["diesel_postgres_pool"] }
serde = "1.0.140"

错误消息

    --> src/main.rs:66:6
     |
66   |     .get_result(c)
     |      ^^^^^^^^^^
     |
     = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`with_auth_rust_rocket_diesel_binary`)
     = note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, &i32>>>`
     = note: 123 redundant requirements hidden
     = note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, &i32>>>`
     = note: required because of the requirements on the impl of `diesel::insertable::InsertValues<table, _>` for `DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, &i32>>>`
     = note: 3 redundant requirements hidden
     = note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `diesel::query_builder::InsertStatement<table, diesel::query_builder::insert_statement::ValuesClause<(DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, &i32>>>, DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::first_name, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>, DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::last_name, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>, DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::user_name, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>, DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::email_address, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>), table>, diesel::query_builder::insert_statement::private::Insert, diesel::query_builder::returning_clause::ReturningClause<(columns::id, columns::first_name, columns::last_name, columns::user_name, columns::email_address)>>`
     = note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<'_, _, _>` for `diesel::query_builder::InsertStatement<table, diesel::query_builder::insert_statement::ValuesClause<(DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, &i32>>>, DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::first_name, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>, DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::last_name, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>, DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::user_name, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>, DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<columns::email_address, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>), table>>`
note: required by a bound in `diesel::RunQueryDsl::get_result`
    --> /Users/me/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-2.0.2/src/query_dsl/mod.rs:1679:15
     |
1679 |         Self: LoadQuery<'query, Conn, U>,
     |               ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `diesel::RunQueryDsl::get_result`

我已经审查了柴油,火箭和铁 rust 的文件,并通过其他例子工作,我可以在网上找到,但仍然不完全得到我做错了什么.事先感谢您的帮助.

我try 创建一个POST方法,该方法使用迪塞尔获取我的用户对象的Json版本,并将其插入到数据库中.

推荐答案

您的项目使用的DIESEL版本(2.0.2)与Rocket_sync_db_Pools提供的DIESEL版本(1.4.8)不匹配.这意味着connection.run(move |c| {中的c只是一个与get_result的预期完全不同的类型,即使这两个类型共享相同的名称.

Rust相关问答推荐

在Rust中创建可变片段的可变片段的最有效方法是什么?

即使参数和结果具有相同类型,fn的TypId也会不同

有条件默认实现

无法在线程之间安全地发送future (&Q;)&错误

rust 蚀生命周期 行为

将大小为零的类型实例存储到空指针中

为什么Rust不支持带关联常量的特征对象?

如何创建一个可变的嵌套迭代器?

习语选项<;T>;到选项<;U>;当T->;U用From定义

`actix-web` 使用提供的 `tokio` 运行时有何用途?

使用 pyo3 将 Rust 转换为 Python 自定义类型

为什么某些类型参数仅在特征边界中使用的代码在没有 PhantomData 的情况下进行编译?

OpenGL 如何同时渲染无纹理的四边形和有纹理的四边形

切片不能被 `usize` 索引?

为什么允许重新分配 String 而不是 *&String

只有一个字符被读入作为词法分析器的输入

为实现特征的所有类型实现显示

Cargo:如何将整个目录或文件包含在功能标志中?

将 (T, ()) 转换为 T 安全吗?

有没有办法在 Rust 中对 BigInt 进行正确的位移?