Compile error with Timestamp using diesel and PostgreSQL

I'm using Rust with diesel crate.

I need to add two timestamp fields for every records to my PostgreSQL database:

CREATE TABLE public.photo
(
id BIGSERIAL PRIMARY KEY NOT NULL,
datadoc JSONB,
created_timestamp TIMESTAMP NOT NULL,
modified_timestamp TIMESTAMP NOT NULL
);
CREATE UNIQUE INDEX photo_id_uindex ON public.photo (id);
CREATE INDEX index_tags ON photo USING GIN ((datadoc->'tags') jsonb_path_ops);
CREATE INDEX photo_created_timestamp_uindex ON public.photo (created_timestamp);
CREATE INDEX photo_modified_timestamp_uindex ON public.photo (modified_timestamp);

(Note: my assumption is it is easier to set and retrieve these timestamps via Rust diesel by having my own fields, rather manipulating/retrieving the row timestamp data provided by PostgreSQL, if any.)

... and the schema.rs that is generated:

table! {
photo (id) {
id -> Int8,
datadoc -> Nullable,
created_timestamp -> Timestamp,
modified_timestamp -> Timestamp,
}
}

and I'm having trouble with my model.rs:

use super::schema::photo;
use serde_json;
use diesel::sql_types::Timestamp;

#[derive(Queryable)]
pub struct Photo {
pub id: i64,
pub datadoc: Option<serde_json::Value>,
pub created_timestamp: Timestamp,
pub modified_timestamp: Timestamp,
}

#[derive(Insertable)]
#[table_name="photo"]
pub struct NewPhoto {
pub datadoc: Option<serde_json::Value>,
pub created_timestamp: Timestamp,
pub modified_timestamp: Timestamp,
}

Compile errors:

error[E0277]: the trait bound diesel::sql_types::Timestamp: diesel::Expression is not satisfied
--> src\models.rs:13:10
|
13 | #[derive(Insertable)]
| ^^^^^^^^^^ the trait diesel::Expression is not implemented for diesel::sql_types::Timestamp
|
= note: required because of the requirements on the impl of diesel::Expression for &diesel::sql_types::Timestamp
= note: required because of the requirements on the impl of diesel::expression::AsExpression<diesel::sql_types::Timestamp> for &diesel::sql_types::Timestamp
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0277]: the trait bound diesel::sql_types::Timestamp: diesel::Expression is not satisfied
--> src\models.rs:13:10
|
13 | #[derive(Insertable)]
| ^^^^^^^^^^ the trait diesel::Expression is not implemented for diesel::sql_types::Timestamp
|
= note: required because of the requirements on the impl of diesel::Expression for &'insert diesel::sql_types::Timestamp
= note: required because of the requirements on the impl of diesel::expression::AsExpression<diesel::sql_types::Timestamp> for &'insert diesel::sql_types::Timestamp
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0277]: the trait bound diesel::sql_types::Timestamp: diesel::Expression is not satisfied
--> src\models.rs:13:10
|
13 | #[derive(Insertable)]
| ^^^^^^^^^^ the trait diesel::Expression is not implemented for diesel::sql_types::Timestamp
|
= note: required because of the requirements on the impl of diesel::Expression for &diesel::sql_types::Timestamp
= note: required because of the requirements on the impl of diesel::expression::AsExpression<diesel::sql_types::Timestamp> for &diesel::sql_types::Timestamp
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 3 previous errors

error: Could not compile source_code.

To learn more, run the command again with --verbose.

Process finished with exit code 101

I'm new to Rust and appreciate if someone could help me. Thanks!

I think you need to use one of the other types listed on the Timestamp page in the documentation for your struct fields, and they'll get converted automatically.

2 Likes

Yup, that worked. Thanks!

Thanks. Saved my time. Created account just to upvote your answer.