今天我一直在关注Rust's Diesel ORMthis walk-through,但我没法得到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"

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
}

(我读到有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))
}

但当我try 使用它时,我会遇到以下错误:

<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
                              ^~~~~~~~~

看起来像是第一个错误,Timestamptzinfer_schema不知道如何解释表中已经存在的Postgresql类型的结果.至于第二个,我想如果显式导入Timestamp类型,我可以用它创建一个Post struct .

有没有什么明显的事情表明我做错了?

顺便说一句,我对Rust 和柴油机使用了相当多的代码生成,所以很容易丢失,但我认为这应该是一个简单的事情来完成.


Edit:

我用timestamp with time zone创建了这个表,它看起来像may not be supported yet:

CREATE TABLE post (
    ...
    created timestamp with time zone NOT NULL,
    updated timestamp with time zone
)

Edit 2:

我将models.rs改为如下所示,并消除了Timestamp未定义的错误.我还意识到,我需要在每个 struct 之上派生#[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
}

推荐答案

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

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

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

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

Postgresql相关问答推荐

PostgreSQL存储过程中不存在游标

Postgres:一次插入多行时自定义upsert(存储过程?)?

数据库所有者无法授予 Select 权限

需要用 jack/pgx 更新 golang 中复合类型的 PSQL 行

如何在 sequelize 使用 db postgres 中通过外键更新记录和更新包含许多记录关系

这个 Supbase 查询在 SQL ( Postgresql ) 中的类似功能是什么

如何将 NULL 值插入 UUID 而不是零

新数据未保存到 Postgres 上的 Rails 数组列

Rails:创建删除表级联迁移

postgresql中的外键可以被触发器违反

为 Django Postgres 连接强制 SSL

django.db.utils.IntegrityError:duplicate key value violates unique constraint "django_content_type_pkey"

psql 致命角色不存在

Rails PG::UndefinedTable:错误:missing FROM-clause entry for table

如何将 PostgreSQL 查询输出导出到 csv 文件

如何在 psql 中设置默认显示模式

postgresql 删除级联

如何确定在 Postgres 中使用什么类型的索引?

Postgresql:syntax error at or near "-"

用 PostgreSQL 中另一个表的列更新一个表的列