7

I'm trying to use Diesel's inference capability to work with a SQLite table that contains a nullable timestamp column. A complete example is available in an older revision of this GitHub project.

The books table is defined as follows:

create table books (
    id integer not null primary key,
    title varchar null,
    save_date timestamp null 
)

I'm trying to query it with the following struct:

pub mod domain {
    use chrono::naive;

    #[derive(Queryable)]
    pub struct Book {
        id : i32,
        title : Option<String>,
        save_date : Option<naive::NaiveDateTime>, 
    }
}

However when I try to actually perform the query, as in:

fn main() {
    use self::schema::books::dsl::*;
    let database_url = env::var("DATABASE_URL").unwrap();
    let conn = sqlite::SqliteConnection::establish(&database_url).unwrap();
    let res = books.load::<domain::Book>(&conn);
}

I get the following error message:

error[E0277]: the trait bound `std::option::Option<chrono::NaiveDateTime>: diesel::types::FromSqlRow<diesel::types::Nullable<diesel::types::Timestamp>, _>` is not satisfied
  --> src/main.rs:32:21
   |
32 |     let res = books.load::<domain::Book>(&conn);
   |                     ^^^^ the trait `diesel::types::FromSqlRow<diesel::types::Nullable<diesel::types::Timestamp>, _>` is not implemented for `std::option::Option<chrono::NaiveDateTime>`
   |
   = help: the following implementations were found:
             <std::option::Option<chrono::naive::date::NaiveDate> as diesel::types::FromSqlRow<diesel::types::Nullable<diesel::types::Date>, DB>>
             <std::option::Option<chrono::naive::time::NaiveTime> as diesel::types::FromSqlRow<diesel::types::Nullable<diesel::types::Time>, DB>>
             <std::option::Option<chrono::naive::datetime::NaiveDateTime> as diesel::types::FromSqlRow<diesel::types::Nullable<diesel::types::Timestamp>, DB>>
             <std::option::Option<bool> as diesel::types::FromSqlRow<diesel::types::Nullable<diesel::types::Bool>, DB>>
           and 26 others
   = note: required because of the requirements on the impl of `diesel::types::FromSqlRow<(diesel::types::Integer, diesel::types::Nullable<diesel::types::Text>, diesel::types::Nullable<diesel::types::Timestamp>), _>` for `(i32, std::option::Option<std::string::String>, std::option::Option<chrono::NaiveDateTime>)`
   = note: required because of the requirements on the impl of `diesel::Queryable<(diesel::types::Integer, diesel::types::Nullable<diesel::types::Text>, diesel::types::Nullable<diesel::types::Timestamp>), _>` for `domain::Book`

What I don't understand is why the implementation for chrono::naive::datetime::NaiveDateTime isn't picked up and what I should do to make it happen.

I'm using the stable toolchain, which is currently 1.18.0, and these are my dependencies:

[dependencies]
chrono = "0.4.0"
diesel = { version = "0.13.0", features = [ "chrono", "sqlite" ] }
diesel_codegen = { version = "0.13.0", features = [ "sqlite" ] }
dotenv = "0.9.0"
Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • I added my dependencies to the original post. As you can see the ``chrono`` feature is defined for ``diesel``. It isn't available for ``diesel_codegen``. – Nicola Musatti Jul 02 '17 at 12:21
  • The hint is in the error. The trait is implemented for `NaiveDateTime` but not `Option` (not that I can see in the Diesel documentation anyway). How you go about implementing it I am unsure - I've not played with Diesel for very long. Perhaps try removing the `Option` to begin with to make sure that is the issue. – Simon Whitehead Jul 02 '17 at 12:46
  • Did you run the migration to setup the SQLite database? Otherwise schema inference won't work – Nicola Musatti Jul 02 '17 at 12:57
  • The error message mentions `` as diesel::types::FromSqlRow, DB>>`` which should match except for the ``datetime`` module, which is private. – Nicola Musatti Jul 02 '17 at 13:00
  • 2
    It should be noted that this question is not a duplicate. This error has nothing to do with multiple conflicting dependencies. The "duplicate" designation should be removed. – phayes Jun 13 '19 at 18:58

0 Answers0