Rust tokio_postgres row to object mapping fails with serde_postgres for timestamp column

1.4k views Asked by At

I have rust actix-web rest project, which uses tokio_postgres and serde_postgres.

The Table

acme=# \d job
                                             Table "public.job"
     Column      |            Type             | Collation | Nullable |               Default               
-----------------+-----------------------------+-----------+----------+-------------------------------------
 created_at      | timestamp without time zone |           | not null | CURRENT_TIMESTAMP

The Struct

#[derive(Debug, Deserialize, Serialize)]
pub struct Job {
    pub created_at: NaiveDateTime,
}

The Function

pub async fn get_all_jobs(&self) -> Result<Vec<Job>, Error> {
        let client = self.pool.get_conn().await?;
        let query = "SELECT created_at FROM job";

        let rows = &client.query(query, &[]).await?;
        // ------------ Following line throws error --------------
        let jobs: Vec<Job> = from_rows(rows)?;

        // ------------ Following lines works fine ---------------
        // let mut jobs: Vec<Job> = Vec::new();
        // for row in rows {
        //    jobs.push(Job {
        //        created_at: row.get(0)
        //    })
        // }

        Ok(jobs)
    }

Cargo.toml

tokio = { version = "0.3", features = ["full"] }
serde = "1.0.117"
serde_derive = "1.0.117"
serde_json = "1.0.60"
serde_postgres = "0.2.0"
thiserror = "1.0.22"
tokio-postgres = { version = "0.5", features = ["with-uuid-0_8", "with-chrono-0_4"] }

The above code executes perfectly when I traverse the rows and map each row to Job struct manually. I don't like this manual approach where serde_postgres::from_rows is designed for just the same task, but it fails for timestamp without time zone type columns only.

I am suspecting this is version mismatch issue, if not I am missing something. Would be a great help if anyone can share some ideas/solutions

1

There are 1 answers

0
Bopsi On BEST ANSWER

It seems serde_postgres crate does not support datetimes. refer this

So I implemented from_rows likes this

impl From<&Row> for Job {
    fn from(row: &Row) -> Self {
        Self {
            created_at: row.get("created_at")
        }
    }
}

This works.