使用 Diesel 从 mySQL 数据库中检索日期时间

Leo*_* Sá 6 mysql rust rust-chrono rust-diesel

我无法使用 Rocket 和 Diesel 从填充的 mySQL 数据库中检索日期时间。

这是我的模型:

extern crate chrono;

use diesel::prelude::*;
use diesel::mysql::MysqlConnection;
use schema::chrisms;
use diesel::sql_types::Datetime;
use self::chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};

#[derive(Serialize, Deserialize, Queryable)]
pub struct Chrisms {
    pub entity_ekklesia_location_id: i32,
    pub serie_number: Option<String>,
    pub seat_number: Option<String>,
    pub date: Datetime,
    pub year: i32,
    pub deleted: bool,
    pub entity_chrism_location_id: Option<i32>,
    pub entity_chrism_location_description: Option<String>,
    pub entity_rel_mec_id: Option<i32>,
    pub entity_rel_mec_description: Option<String>,
    pub created_by_user_id: Option<i32>,
    pub updated_by_user_id: Option<i32>,
    pub deleted_by_user_id: Option<i32>,
    pub created_at: Datetime,
    pub updated_at: Datetime,
    pub id: i32,
}

impl Chrisms {
    pub fn read(connection: &MysqlConnection) -> Vec<Chrisms> {
        chrisms::table.load::<Chrisms>(connection).unwrap()
    }
}
Run Code Online (Sandbox Code Playgroud)

我的架构:

table! {
    chrisms (id) {
        entity_ekklesia_location_id -> Integer,
        serie_number -> Nullable<Varchar>,
        seat_number -> Nullable<Varchar>,
        date -> Datetime,
        year -> Integer,
        deleted -> Bool,
        entity_chrism_location_id -> Nullable<Integer>,
        entity_chrism_location_description -> Nullable<Varchar>,
        entity_rel_mec_id -> Nullable<Integer>,
        entity_rel_mec_description -> Nullable<Varchar>,
        created_by_user_id -> Nullable<Integer>,
        updated_by_user_id -> Nullable<Integer>,
        deleted_by_user_id -> Nullable<Integer>,
        created_at -> Datetime,
        updated_at -> Datetime,
        id -> Integer,
    }
}
Run Code Online (Sandbox Code Playgroud)

这会产生错误:

1. the trait `_IMPL_SERIALIZE_FOR_TemplateContext::_serde::Serialize` is not 
implemented for `diesel::sql_types::Datetime`
-required by `_IMPL_SERIALIZE_FOR_TemplateContext::_serde::ser::SerializeStruct::serialize_field`

2. the trait `_IMPL_SERIALIZE_FOR_TemplateContext::_serde::Deserialize<'_>` is 
not implemented for `diesel::sql_types::Datetime`
- required by `_IMPL_SERIALIZE_FOR_TemplateContext::_serde::de::SeqAccess::next_element`
- required by `_IMPL_SERIALIZE_FOR_TemplateContext::_serde::de::MapAccess::next_value`

3. the trait `diesel::Queryable<diesel::sql_types::Datetime, 
diesel::mysql::Mysql>` is not implemented for `diesel::sql_types::Datetime`
- required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<_, models::chrisms::Chrisms>` for `schema::chrisms::table`
Run Code Online (Sandbox Code Playgroud)

我该如何解决?我测试了一堆用途,例如diesel:mysql_typesrocket:config等等,似乎不是问题所在。

pyl*_*int 6

使用日期时间创建/读取/更新/删除示例

货物.toml:

[dependencies]
diesel = { version = "1.4", features = ["sqlite", "chrono"] }
chrono = "0.4"
Run Code Online (Sandbox Code Playgroud)

用户架构:

[dependencies]
diesel = { version = "1.4", features = ["sqlite", "chrono"] }
chrono = "0.4"
Run Code Online (Sandbox Code Playgroud)
CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    email TEXT NOT NULL UNIQUE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
Run Code Online (Sandbox Code Playgroud)

输出示例:

CRUD - Create
[src/main.rs:85] &last_insert_user = User {
    id: 1,
    email: "test+1606720099@example.com",
    created_at: 2020-11-30T07:08:19,
}

CRUD - Read
[src/main.rs:88] read_users(&conn)? = [
    User {
        id: 1,
        email: "test+1606720099@example.com",
        created_at: 2020-11-30T07:08:19,
    },
]

CRUD - Update
[src/main.rs:93] read_users(&conn)? = [
    User {
        id: 1,
        email: "test+1606720099@example.com",
        created_at: 2020-11-30T07:08:19.386513,
    },
]

CRUD - Delete
[src/main.rs:98] read_users(&conn)? = []
Run Code Online (Sandbox Code Playgroud)