我正在尝试使用 Diesel 的推理功能来处理包含可为空的时间戳列的 SQLite 表。此GitHub 项目的旧版本中提供了完整示例。
该books
表定义如下:
create table books (
id integer not null primary key,
title varchar null,
save_date timestamp null
)
我正在尝试使用以下结构查询它:
pub mod domain {
use chrono::naive;
#[derive(Queryable)]
pub struct Book {
id : i32,
title : Option<String>,
save_date : Option<naive::NaiveDateTime>,
}
}
但是,当我尝试实际执行查询时,如下所示:
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);
}
我收到以下错误消息:
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`
我不明白为什么没有执行 forchrono::naive::datetime::NaiveDateTime
以及我应该做些什么来实现它。
我正在使用稳定的工具链,目前是 1.18.0,这些是我的依赖项:
[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"