首页 > 解决方案 > 特性`diesel::Insertable` 未实现

问题描述

我正在尝试使用柴油编写一个简单的插入语句,但我遇到了问题:

特征diesel::Insertableschema::tbl::table 未实现

我的插入看起来像:

pub fn insert_trades(trades : Struct) {

use super::super::schema::tbl::dsl::*;

let conn : &PgConnection = &establish_connection().get().unwrap();

insert_into(tbl).values(&trades).get_result(conn);

}

我的结构看起来像:

#[derive(Debug, Serialize, Deserialize, Insertable, Queryable)]

#[table_name = "tbl"]

pub struct Struct {

pub trade_id: Option<String>,

pub event_time: Option<i64>,

pub event_type: Option<String>,

pub trade_time: Option<i64>,

pub symbol: Option<String>,

pub buyer_id: Option<i64>,

pub seller_id: Option<i64>,

pub price: Option<String>,

pub quantity: Option<String>

}

Schema.rs

table! {

tbl (trade_id) {

trade_id -> Varchar,

event_time -> Int8,

event_type -> Varchar,

trade_time -> Int8,

symbol -> Varchar,

buyer_id -> Int8,

seller_id -> Int8,

price -> Varchar,

quantity -> Varchar,

}

}

编译器抛出的问题是:

error[E0277]: the trait bound `&my_trader::models::trade::Struct: diesel::Insertable<schema::tbl::table>` is not satisfied
 --> src/service/trade_service.rs:8:40
  |
8 |     insert_into(tbl).values(&trades).get_result(conn);
  |                                        ^^^^^^^ the trait `diesel::Insertable<schema::tbl::table>` is not implemented for `&my_trader::models::trade::Struct`
  |
  = help: the following implementations were found:
            <&'insert my_trader::models::trade::Struct as diesel::Insertable<my_trader::schema::tbl::table>>
            <my_trader::models::trade::Struct as diesel::Insertable<my_trader::schema::tbl::table>>

我不确定我做错了什么。我试图用谷歌搜索,但没有运气,而且对 rust 世界很陌生,所以不知道问题出在哪里。谢谢!

标签: postgresqlrustrust-cargoserderust-diesel

解决方案


我发现了这个问题。基本上Struct我试图导入的是一个完全不同的包。虽然它指的是正确的文件,但当我更改导入时crate::,它工作得非常好。之前它指的my_trader::是由 IDE 自动完成的文件。


推荐阅读