使用柴油和杜松插入值时出现奇怪的错误

Weird error when inserting a value with diesel and juniper

我目前正在使用 actix-web、juniper 和 diesel 制作一个小型后端。 它主要基于本教程:https://dev.to/youroff/ultra-fast-backend-with-rust-26ac 但我做了一些更改。

我完成了 API 的“读取”部分,但“创建”部分出现错误。

impl MutationRoot {
    fn create_contraption(
        context: &Context,
        new_contraption: ContraptionInput,
    ) -> FieldResult<Contraption> {
        use crate::schema::contraptions;

        let conn = context.dbpool.get().map_err(|_| {
            FieldError::new("Could not open connection to the database", Value::null())
        })?;

        diesel::insert_into(contraptions::table)
            .values(&new_contraption)
            .get_result(&conn)
            .expect("Could not create new contraptions")
    }
}

这是 cargo check

的输出
    Checking redstone_contraptions_backend v0.1.0 (/home/stowy/Documents/dev/rust/Redstone-Contraptions/redstone_contraptions_backend)
error[E0277]: the trait bound `Result<Contraption, FieldError>: Queryable<(Integer, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Nullable<diesel::sql_types::Text>, diesel::sql_types::Nullable<diesel::sql_types::Text>), Pg>` is not satisfied
  --> src/models/root.rs:46:14
   |
46 |             .get_result(&conn)
   |              ^^^^^^^^^^ the trait `Queryable<(Integer, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Nullable<diesel::sql_types::Text>, diesel::sql_types::Nullable<diesel::sql_types::Text>), Pg>` is not implemented for `Result<Contraption, FieldError>`
   |
   = note: required because of the requirements on the impl of `LoadQuery<PooledConnection<ConnectionManager<PgConnection>>, Result<Contraption, FieldError>>` for `InsertStatement<table, ValuesClause<(ColumnInsertValue<columns::name, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>, ColumnInsertValue<columns::description, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>, ColumnInsertValue<columns::image, diesel::expression::bound::Bound<diesel::sql_types::Nullable<diesel::sql_types::Text>, &std::string::String>>, ColumnInsertValue<columns::itemslist, diesel::expression::bound::Bound<diesel::sql_types::Nullable<diesel::sql_types::Text>, &std::string::String>>), table>>`

据我了解,.get_result(&conn) 类型为 Result<Contraption, FieldError> 的输出未实现 Queryable。但这是正常的,因为它是 Result 类型而不是 Contraption,这就是为什么我在后面放一个 .expect()

并且 Contraption 实施 Queryable :

#[derive(Default, Queryable)]
pub struct Contraption {
    pub id: i32,
    pub name: String,
    pub description: String,
    pub image: Option<String>,
    pub itemslist: Option<String>,
}

#[derive(GraphQLInputObject, Insertable)]
#[graphql(description = "Contraption Input")]
#[table_name = "contraptions"]
pub struct ContraptionInput {
    pub name: String,
    pub description: String,
    pub image: Option<String>,
    pub itemslist: Option<String>,
}

为了以防万一,这是我的依赖项:

[dependencies]
actix = "0.12.0"
actix-web = "3.3.2"
juniper = "0.15.7"
diesel = { version = "1.4.7", features = ["postgres", "r2d2"] }
dotenv = "0.15.0"
r2d2 = "0.8.9"
serde_json = "1.0.67"

我做错了什么?

我只需要添加 .map_err() 而不是 .expect() 并精确 .get_result().

的输出类型
diesel::insert_into(contraptions::table)
    .values(&new_contraption)
    .get_result::<Contraption>(&conn)
    .map_err(|_| FieldError::new("Error creating contraption", Value::null()))