【发布时间】: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(&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"
我做错了什么?
【问题讨论】:
标签: rust rust-diesel