skip to Main Content

I have code that is attempting to pass an array of chrono::DateTime values to a stored procedure in postgres.

let stmt = connection.prepare_cached("call proc($1)").await.unwrap();
if let Ok(_) = connection.execute(&stmt, &[&array]).await {
    info!("worked");
} else {
    error!("error");
}

but I am getting this error:

[Error { kind: ToSql(2), cause: Some(WrongType { postgres: TimestampArray, rust: "&[chrono::datetime::DateTime<chrono::offset::utc::Utc>]" }) }

2

Answers


  1. Chosen as BEST ANSWER

    @pgzlan was correct. change from chrono::DateTime to NaiveDateTime worked.


  2. Based on the error, it’s probably a mismatch between the Rust type and the PostgreSQL type when attempting to pass an array of chrono::DateTime values to a stored procedure.

    You can try converting it before passing it

    use chrono::{DateTime, Utc};
    use postgres_types::ToSql;
    
    // Convert the array of `chrono::DateTime` values
    let converted_dt: Vec<_> = date_array.iter().map(|dt| dt.naive_utc()).collect();
    
    // Or try this:
    // let converted_dt: Vec<String> = date_array.iter().map(|dt| dt.to_rfc3339()).collect();
    
    
    let stmt = connection.prepare_cached("call proc($1)").await.unwrap();
    if let Ok(_) = connection.execute(&stmt, &[&naive_datetimes]).await {
        info!("worked");
    } else {
        error!("error");
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search