Rust Diesel:特征绑定 `NaiveDateTime: Deserialize<'_>` 不满足

Posted

技术标签:

【中文标题】Rust Diesel:特征绑定 `NaiveDateTime: Deserialize<\'_>` 不满足【英文标题】:Rust Diesel: the trait bound `NaiveDateTime: Deserialize<'_>` is not satisfiedRust Diesel:特征绑定 `NaiveDateTime: Deserialize<'_>` 不满足 【发布时间】:2021-03-30 09:01:20 【问题描述】:

我是生锈和柴油的新手。并尝试使用 Rocket 框架创建一个小型演示 api。 出现错误:特征绑定 NaiveDateTime: Deserialize&lt;'_&gt; 不满足

我用谷歌搜索了一些有用的链接,例如:https://github.com/serde-rs/serde/issues/759 好像是版本有问题。

这是我的文件:schema.rs

table! 
    department (dept_id) 
        dept_id -> Int4,
        dept_name -> Nullable<Text>,
        created_on -> Nullable<Timestamp>,
        created_by -> Nullable<Text>,
        modified_on -> Nullable<Timestamp>,
        modified_by -> Nullable<Text>,
        is_active -> Nullable<Bool>,
    

cargo.toml

[dependencies]
diesel =  version = "1.4.5", features = ["postgres","chrono","numeric"] 
dotenv = "0.15.0"
chrono =  version = "0.4.19" 
bigdecimal =  version = "0.1.0" 
rocket = "0.4.6"
rocket_codegen = "0.4.6"
r2d2-diesel = "1.0.0"
r2d2 = "0.8.9"
serde =  version = "1.0.118", features = ["derive"] 
serde_derive = "1.0.118"
serde_json = "1.0.60"

[dependencies.rocket_contrib]
version = "*"
default-features = false
features = ["json"]

model.rs

#![allow(unused)]
#![allow(clippy::all)]

use super::schema::department;
use serde::Serialize;
use serde::Deserialize;

use chrono::NaiveDateTime;
use bigdecimal::BigDecimal;
#[derive(Queryable, Debug, Identifiable, Serialize, Deserialize)]
#[primary_key(dept_id)]
#[table_name = "department"]
pub struct Department 
    pub dept_id: i32,
    pub dept_name: Option<String>,
    pub created_on: Option<NaiveDateTime>,
    pub created_by: Option<String>,
    pub modified_on: Option<NaiveDateTime>,
    pub modified_by: Option<String>,
    pub is_active: Option<bool>,

ma​​in.rs

#[macro_use]
extern crate diesel;
extern crate dotenv;
extern crate serde;
extern crate serde_derive;
extern crate serde_json;
extern crate chrono;
extern crate bigdecimal;

mod models;
mod schema;
mod connection;

fn main() 
    println!("Hello, Home!");

有人可以帮我吗? 谢谢!

【问题讨论】:

【参考方案1】:

Chrono 有一些 optional features 必须启用。 在这种情况下,您需要在 Cargo.tomlchrono 依赖项配置中包含 serde 作为可选功能:

chrono =  version = "0.4", features = ["serde"] 

【讨论】:

以上是关于Rust Diesel:特征绑定 `NaiveDateTime: Deserialize<'_>` 不满足的主要内容,如果未能解决你的问题,请参考以下文章

Rust Diesel 无法编译并出现链接器错误

返回使用 Rocket 和 Diesel (Rust) 在 PostgreSQL 中创建的单个记录

使用 r2d2 在 rust/diesel 应用程序中实现连接池

Rust Diesel Abstract 更新函数

Rust Diesel 未构建错误使用未声明的板条箱或模块

Rust Diesel 原始 SQL 给出错误“`std::result::Result<Vec<T>,diesel::result::Error>` 所需的类型注释”