postgresql - 特性`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 世界很陌生,所以不知道问题出在哪里。谢谢!
解决方案
我发现了这个问题。基本上Struct
我试图导入的是一个完全不同的包。虽然它指的是正确的文件,但当我更改导入时crate::
,它工作得非常好。之前它指的my_trader::
是由 IDE 自动完成的文件。
推荐阅读
- python - 编写一个函数,报告文件中 a 和 b 的数量。然后打印一条语句,指示是否有更多的 a 或 b
- jmeter - Jmeter 日志在 html 日志中显示每个线程组的详细信息
- java - 你如何参数化 Spring Boot Gradle 插件?
- windows - 如何在 Windows 容器的 aspnet:3.0 基础映像中安装根证书
- arrays - 使用数组比较 2 个范围
- c# - 你能帮我解决多对一的关系吗?
- java - @Service 类是否应该与@Controller 类具有相同的接口?
- css - Safari 13+ box-shadow 仅在内容不为空时显示
- ruby - 检查是否定义了在 Proc 中编写的方法?
- python - Pyomo:隐式替换 Component 属性