Retrieve nullable Timestamp using postgres crate

I am querying a PostgreSQL db using the postgres crate (postgres - Rust).

Many of the fields have null values, to which I treat like this:

let nullable_int_var: Option<i32> = row.get("int_field");

let int_var = match nullable_int_var{

            Some(x) => x,

            None => 0,

};

Which are usually fine.

However, when I tried to do the same to a 'timestamp without timezone' field, with

let nullable_datetime_var: Option<chrono::NaiveDate> = row.get("timestamp_field");

I get the following error from the compiler:


the trait bound `chrono::NaiveDate: postgres::types::FromSql<'_>` is not satisfied

the trait `postgres::types::FromSql<'_>` is not implemented for `chrono::NaiveDate`

note: required because of the requirements on the impl of `postgres::types::FromSql<'_>` for `std::option::Option<chrono::NaiveDate>`rustc(E0277)

How should I treat a timestamp without timezone Postgres field that can be null?

Have you enabled the with-chrono-0_4 feature of the postgres crate? It's necessary to do so for FromSql to be implemented for third-party types.

1 Like