Search code examples
postgresqlrustrust-cargoserderust-diesel

The trait `diesel::Insertable<schema::trd::table>` is not implemented for <Struct>


I am trying to write a simple insert statement using diesel but something I am getting the issue:

The trait diesel::Insertableschema::tbl::table is not implemented for

My insert looks like:

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);

}

My struct looks like:

#[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,

}

}

The issue which compiler throw is :

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>>

I am not really sure what I am doing wrong. I tried to google but with no luck, and very new to the rust world so couldn't get what the issue is. Thanks!


Solution

  • I found the issue. Basically the Struct I was trying to import was a different package altogether. Although it was referring to the correct file, when I changed the import like crate::, it worked perfectly fine. Earlier it was referring to the file like my_trader:: which was automatically done by the IDE.