Rust的柴油图书馆与Postgres的时间戳

ere*_*wok 18 postgresql rust rust-diesel

今天我一直在看看Rust的Diesel ORM,继续这个演练,我无法Timestamp上班.

Cargo.toml

[dependencies]
diesel = { version = "0.6.2", features = ["chrono"] }
diesel_codegen = { version = "0.6.2", default-features = false, features = ["nightly", "postgres"] }
dotenv = "0.8.0"
dotenv_macros = "0.8.0"
Run Code Online (Sandbox Code Playgroud)

models.rs

#[derive(Queryable)]

pub struct Author {
    pub id: i32,
    pub first_name: String,
    pub last_name: String,
    pub email: String
}

pub struct Post {
    pub id: i32,
    pub author: Author,
    pub title: String,
    pub body: String,
    pub published: bool,
    pub created: Timestamp,
    pub updated: Timestamp
}
Run Code Online (Sandbox Code Playgroud)

(我读到有一种diesel::types::Timestamp类型)

lib.rs

#![feature(custom_derive, custom_attribute, plugin)]
#![plugin(diesel_codegen, dotenv_macros)]

#[macro_use]
extern crate diesel;
extern crate dotenv;

pub mod schema;
pub mod models;

use diesel::prelude::*;
use diesel::types::Timestamp;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;

pub fn establish_connection() -> PgConnection {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL").
        expect("DATABASE_URL must be set");
    PgConnection::establish(&database_url).
        expect(&format!("Error connecting to {}", database_url))
}
Run Code Online (Sandbox Code Playgroud)

但这些是我尝试使用它时遇到的错误:

<diesel macros>:5:1: 5:71 note: in this expansion of table_body! (defined in <diesel macros>)
src/schema.rs:1:1: 1:40 note: in this expansion of table! (defined in <diesel macros>)
src/schema.rs:1:1: 1:40 note: in this expansion of infer_schema! (defined in src/lib.rs)
src/lib.rs:1:1: 1:1 help: run `rustc --explain E0412` to see a detailed explanation
src/lib.rs:1:1: 1:1 help: no candidates by the name of `Timestamptz` found in your project; maybe you misspelled the name or forgot to import an external crate?
src/lib.rs:1:1: 1:1 error: type name `Timestamptz` is undefined or not in scope [E0412]
src/lib.rs:1 #![feature(custom_derive, custom_attribute, plugin)]

...

<diesel macros>:38:1: 38:47 note: in this expansion of column! (defined in <diesel macros>)
<diesel macros>:5:1: 5:71 note: in this expansion of table_body! (defined in <diesel macros>)
src/schema.rs:1:1: 1:40 note: in this expansion of table! (defined in <diesel macros>)
src/schema.rs:1:1: 1:40 note: in this expansion of infer_schema! (defined in src/lib.rs)
src/lib.rs:1:1: 1:1 help: run `rustc --explain E0412` to see a detailed explanation
src/lib.rs:1:1: 1:1 help: no candidates by the name of `Timestamptz` found in your project; maybe you misspelled the name or forgot to import an external crate?
src/models.rs:16:18: 16:27 error: type name `Timestamp` is undefined or not in scope [E0412]
src/models.rs:16     pub created: Timestamp,
                              ^~~~~~~~~
src/models.rs:16:18: 16:27 help: run `rustc --explain E0412` to see a detailed explanation
src/models.rs:16:18: 16:27 help: you can import it into scope: `use diesel::types::Timestamp;`.
src/models.rs:17:18: 17:27 error: type name `Timestamp` is undefined or not in scope [E0412]
src/models.rs:17     pub updated: Timestamp
                              ^~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

它看起来像第一个错误,Timestamptzinfer_schema不知道如何解释Postgresql类型的结果,该类型已经在表中.至于第二个,我想也许如果明确导入那个Timestamp类型,我可以Post用它创建一个结构.

有什么明显的东西我在这里做错了吗?

顺便说一下,我对Rust和Diesel很新,使用了相当多的代码生成,因此很容易迷失,但我认为这应该是一个简单易行的事情.


编辑:

我曾经timestamp with time zone创建过表,看起来可能还没有支持:

CREATE TABLE post (
    ...
    created timestamp with time zone NOT NULL,
    updated timestamp with time zone
)
Run Code Online (Sandbox Code Playgroud)

编辑2:

我改变了models.rs看起来像下面这个并且摆脱了关于Timestamp未定义的错误.我也意识到我需要#[derive(Queryable)]在每个结构上面得到.以下编译正常,但以前的错误Timestamptz仍然存在:

use diesel::types::Timestamp;

#[derive(Queryable)]
pub struct Author {
    pub id: i32,
    pub first_name: String,
    pub last_name: String,
    pub email: String
}

#[derive(Queryable)]
pub struct Post {
    pub id: i32,
    pub author: Author,
    pub title: String,
    pub body: String,
    pub published: bool,
    pub created: Timestamp,
    pub updated: Timestamp
}
Run Code Online (Sandbox Code Playgroud)

sgr*_*rif 25

所有类型diesel::sql_types都是标记,用于表示模式的各种SQL数据类型.它们永远不应该用在你自己的结构中.你需要的是它实现了一个类型diesel::deserialize::FromSql<diesel::sql_types::Timestamp, diesel::pg::Pg>(文件:FromSql,Timestamp,Pg).有两种类型可以实现这种特性.

第一个是std::time::SystemTime不需要额外的依赖,但没有很多功能.

第二是chrono::NaiveDateTime.这可能是你想要的类型.为了使用它,您需要添加chrono到您的依赖项,并更改Cargo.toml中的柴油线以包含计时功能,所以它看起来像diesel = { version = "0.7.0", features = ["postgres", "chrono"] }

(从技术上讲,有第三种类型,diesel::data_types::PgTimestamp但这几乎肯定不是你想要的,因为该结构只是数据库中时间戳的文字表示,因此其他类型不必担心原始字节)

  • [此处](https://kotiri.com/2018/01/31/postgresql-diesel-rust-types.html) 提供了 Diesel、PostgreSQL 和 Rust 数据类型映射的精彩汇编 (3认同)