【问题标题】:Weird error when inserting a value with diesel and juniper使用柴油和杜松插入值时出现奇怪的错误
【发布时间】:2021-11-02 12:43:40
【问题描述】:

我目前正在使用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(&amp;conn) 的输出类型为Result&lt;Contraption, FieldError&gt; 并没有实现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"

我做错了什么?

【问题讨论】:

    标签: rust rust-diesel


    【解决方案1】:

    我只需要添加一个.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()))
    

    【讨论】:

      猜你喜欢
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 2021-07-21
      • 2020-08-30
      • 2013-05-05
      • 2014-09-24
      相关资源
      最近更新 更多