Hi all,
Getting back into Rust after about a year. Trying to work with the actix-web and diesel libraries. Following along with the example here: https://github.com/actix/examples/blob/master/diesel/src/db.rs
However I’m running into the following error:
error: the `transaction` method cannot be invoked on a trait object
--> src/db.rs:49:11
|
49 | *conn.transaction::<_, actix_web::error::ResponseError, _>(|| {
| ^^^^^^^^^^^
|
= note: another candidate was found in the following trait, perhaps add a `use` for it:
`use diesel::Connection;`
What is the proper way to deref the conn
so that I can call the transaction
function?
Here is the code file which is causing the issue:
use actix::prelude::*;
use actix_web::*;
use diesel;
use diesel::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool};
use models;
use schema;
/**
* This is the DB executor actor. We are going to run 3 of them in parallel.
*/
pub struct DbExecutor(pub Pool<ConnectionManager<MysqlConnection>>);
pub struct CreateMessageIntent {
pub batch_id: String,
pub message_type_id: u16,
pub xref: String,
pub pid: String,
pub body: Vec<u8>,
}
impl Message for CreateMessageIntent {
type Result = Result<models::SendMessageIntent, Error>;
}
impl Actor for DbExecutor {
type Context = SyncContext<Self>;
}
impl Handler<CreateMessageIntent> for DbExecutor {
type Result = Result<models::SendMessageIntent, Error>;
fn handle(&mut self, msg: CreateMessageIntent, _: &mut Self::Context) -> Self::Result {
use self::schema::send_message_intent::dsl::*;
let new_intent = models::NewSendMessageIntent {
batch_id: &msg.batch_id,
message_type_id: msg.message_type_id,
xref: &msg.xref,
pid: &msg.pid,
body: &msg.body,
};
let conn: &MysqlConnection = &self.0.get().unwrap();
*conn.transaction::<_, actix_web::error::ResponseError, _>(|| {
diesel::insert_into(send_message_intent)
.values(new_intent)
.execute(conn)
.map_err(|_| {
error::ErrorInternalServerError(
"Error inserting send message intent"
)
})?;
send_message_intent
.order(id.desc())
.first(conn)
.map_err(|_| {
error::ErrorInternalServerError(
"Error loading new send message intent"
)
})?
}).map_err(|_| {
error::ErrorInternalServerError(
"Error inserting send message intent"
)
})?
}
}