29

我今天一直在按照这个演练查看 Rust 的Diesel ORM,但我无法工作。Timestamp

货运.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"

模型.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类型)

库文件

#![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))
}

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

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

看起来第一个错误Timestamptz是因为infer_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
)

编辑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
}
4

2 回答 2

44

中的所有类型diesel::sql_types都是表示架构的各种 SQL 数据类型的标记。永远不要在您自己的结构中使用它们。您需要的是一种实现diesel::deserialize::FromSql<diesel::sql_types::Timestamp, diesel::pg::Pg>(文档:、、、FromSqlTimestamp的类型Pg。有两种类型可以实现该特征。

第一个是std::time::SystemTime不需要额外的依赖项,但没有大量的功能。

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

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

于 2016-08-17T11:10:31.773 回答
0

请检查 ui 中的数据类型

“src/models.rs:16:18: 16:27 帮助:您可以将其导入范围:use diesel::types::Timestamp;. src/models.rs:17:18: 17:27 错误:类型名称Timestamp未定义或不在范围内 [E0412] src/models.rs:17 发布更新:时间戳“

也许时间戳不是定义词。

于 2016-07-30T20:29:57.563 回答