rust 发布方法query_builder::查询片段时出错/ query_builder::查询片段

iqxoj9l9  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(182)

我是一个相对较新的生 rust ,并真的很喜欢玩它。但是,我坚持在我的CRUD应用程序使用柴油和火箭错误。我有一个main.rs,model.rs和schema.rs。
我的POST方法使用了我创建的User结构体,但出现错误。
我正在使用一个postgres数据库,我有运行在后台的码头,柴油和火箭的路由。
我的models.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,
}

我的main.rs(包括了所有细节,但真正的问题是关于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 的文档,并通过其他例子工作,我可以在网上找到什么,但仍然不太明白我做错了什么。提前感谢任何帮助。
我尝试创建一个post方法,它使用Diesel获取User对象的Json版本并将其插入到数据库中。

tnkciper

tnkciper1#

您的项目所使用的diesel版本(2.0.2)与rocket_sync_db_pools所提供的diesel版本(1.4.8)不匹配。这意味着connection.run(move |c| {中的cget_result所期望的类型完全不同,即使这些类型共享相同的名称。

相关问题