diesel: The trait bound `chrono::NaiveDateTime: diesel::Expression` is not satisfied

  • Which versions of Rust and Diesel are you using? Rust 1.18.0, Diesel 0.13.0.

  • Which feature flags are you using?

diesel = { version = "0.13", features = ["postgres", "chrono"] }
diesel_codegen = { version = "0.13", features = ["postgres"] }
chrono = { version = "0.4", features = ["serde"] }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
  • What are you trying to accomplish?

Trying to upgrade from Diesel 0.11, Serde 0.9 and Chrono 0.3 to the above versions.

  • What is the full error you are seeing?

I’m seeing the following for each AsChangeset that uses chrono::NaiveDateTime:

error[E0277]: the trait bound `chrono::NaiveDateTime: diesel::Expression` is not satisfied
  --> src/models/emails.rs:37:10
   |
37 | #[derive(AsChangeset, Clone, Debug)]
   |          ^^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `chrono::NaiveDateTime`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&chrono::NaiveDateTime`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::types::Timestamp>` for `&chrono::NaiveDateTime`
   = note: this error originates in a macro outside of the current crate
  • How can we reproduce this?

I think this should be reproducible with the above versions of Chrono and Diesel by having a table that has a column of type chrono::NaiveDateTime, then having an AsChangeset struct that tries to update said NaiveDateTime column.

I’m using NaiveDateTime in Rust for Postgres’ TIMESTAMP:

updated_at TIMESTAMP NOT NULL default now()

I’ve been going round in circles with this, but if it isn’t actually a Diesel issue then I apologise in advance. Thanks for all the work on Diesel, it’s excellent! 😃

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 8
  • Comments: 22 (11 by maintainers)

Most upvoted comments

@killercup You’re a gentleman and a scholar! Thanks so much for taking the time to help!


For anyone else who finds themselves here:

Add Diesel and co. as you would usually:

diesel = { version = "0.13.0", features = ["postgres", "chrono"] }
diesel_codegen = { version = "0.13.0", features = ["postgres"] }
r2d2 = "0.7"
r2d2-diesel = "0.13.0"

As @killercup said, replace the version of Diesel you’re using like so:

[replace]
"diesel:0.13.0" = { git = "https://github.com/diesel-rs/diesel" }

Run cargo update to make sure r2d2-diesel is using the latest version of Diesel it can.

Build.


Thanks again @killercup 😄

Does cargo tree give you two diesel versions? I’d assume so, because r2d2-diesel uses the latest diesel, not its master branch.

Maybe a better way to get this going is to not specify diesel master, but 0.13.0 and adding

[replace]
"diesel:0.13.0" = { git = "https://github.com/diesel-rs/diesel" }

This replace every occurrence of diesel 0.13 in your dependency graph. (You might need to add the same lines for tje other diesel-* crates.)

Thanks! I can’t take credit for it though, I just filled in the details you ask for 😃

lol 😅

@killercup a very good notice, I have just found this information too (about it’s master branch), but thanks for such a nice help here anyway!

@sgrif Actually, I use 0.14 and 0.14.1 now and I have these errors:

error[E0277]: the trait bound `chrono::NaiveDateTime: phone::_IMPL_DESERIALIZE_FOR_PhoneInfo::_serde::Serialize` is not satisfied
 --> src/models.rs:9:88
  |
9 | #[derive(Debug, Clone, Queryable, Identifiable, Insertable, Associations, AsChangeset, Serialize, Deserialize)]
  |                                                                                        ^^^^^^^^^ the trait `phone::_IMPL_DESERIALIZE_FOR_PhoneInfo::_serde::Serialize` is not implemented for `chrono::NaiveDateTime`
  |
  = note: required by `phone::_IMPL_DESERIALIZE_FOR_PhoneInfo::_serde::ser::SerializeStruct::serialize_field`

error[E0277]: the trait bound `chrono::NaiveDateTime: phone::_IMPL_DESERIALIZE_FOR_PhoneInfo::_serde::Deserialize` is not satisfied
 --> src/models.rs:9:99
  |
9 | #[derive(Debug, Clone, Queryable, Identifiable, Insertable, Associations, AsChangeset, Serialize, Deserialize)]
  |                                                                                                   ^^^^^^^^^^^ the trait `phone::_IMPL_DESERIALIZE_FOR_PhoneInfo::_serde::Deserialize` is not implemented for `chrono::NaiveDateTime`
  |
  = note: required by `phone::_IMPL_DESERIALIZE_FOR_PhoneInfo::_serde::de::SeqVisitor::visit`

error[E0277]: the trait bound `chrono::NaiveDateTime: phone::_IMPL_DESERIALIZE_FOR_PhoneInfo::_serde::Deserialize` is not satisfied
 --> src/models.rs:9:99
  |
9 | #[derive(Debug, Clone, Queryable, Identifiable, Insertable, Associations, AsChangeset, Serialize, Deserialize)]
  |                                                                                                   ^^^^^^^^^^^ the trait `phone::_IMPL_DESERIALIZE_FOR_PhoneInfo::_serde::Deserialize` is not implemented for `chrono::NaiveDateTime`
  |
  = note: required by `phone::_IMPL_DESERIALIZE_FOR_PhoneInfo::_serde::de::MapVisitor::visit_value`

error: aborting due to 3 previous errors

error: Could not compile `omp-mdm-server`.

To learn more, run the command again with --verbose.
#[derive(Debug, Clone, Queryable, Identifiable, Insertable, Associations, AsChangeset, Serialize, Deserialize)]
#[table_name = "phones"]
#[primary_key(imei)]
#[belongs_to(User)]
pub struct Phone {
    pub imei: i64,
    pub model: String,
    pub vendor: String,
    pub manufacturer: String,
    pub last_sync: NaiveDateTime, // In UTC
    pub user_id: i32,
}
[dependencies]
rocket = "0.2.8"
rocket_codegen = "0.2.8"
rocket_contrib = "0.2.8"
serde = "0.9"
serde_derive = "0.9"
serde_json = "0.9"
r2d2 = "0.7.2"
r2d2-diesel = "0.14"
chrono = { version = "0.4", features = ["serde"] }

[dependencies.diesel]
version = "0.14"
default-features = false
features = ["sqlite", "chrono"]

[dependencies.diesel_codegen]
version = "0.14"
default-features = false
features = ["sqlite"]

Thank you! I’m glad this works for you. With the next release of Diesel, 0.14, you should only need to increment the diesel and r2d2 version, remove the [replace], and it should all just work™ 😃

Very nice issue description! (We should steal this and make it a template!)

I think your problem stems from chrono recently yanking its 0.3.1 version and releasing 0.4 with serde 1.0 support instead (this was a breaking change in 0.3.1!). Diesel 0.13.0 does not support chrono 0.4. Which means two versions of chrono will be built, and they are not compatible to each other.

Can you try using diesel master instead?

diesel = { git = "https://github.com/diesel-rs/diesel", features = ["postgres", "chrono"] }