简体   繁体   English

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

[英]Timestamp in Rust's Diesel Library with Postgres

I have been taking a look at Rust's Diesel ORM today by following along on this walk-through , and I can't get a Timestamp to work. 今天我一直在看看Rust的Diesel ORM ,继续这个演练 ,我无法让Timestamp工作。

Cargo.toml 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 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
}

(I read that there's a diesel::types::Timestamp type) (我读到有diesel::types::Timestamp类型)

lib.rs 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))
}

But these are the errors I get when I try to use it: 但这些是我尝试使用它时遇到的错误:

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

It looks like the first error, Timestamptz is a result of infer_schema not knowing how to interpret that Postgresql type, which is already in the table. 它看起来像第一个错误, Timestamptzinfer_schema的结果,不知道如何解释已经在表中的Postgresql类型。 As for the second, I thought perhaps if explicitly imported that Timestamp type, I could create a Post struct with it. 至于第二个,我想也许如果显式导入了Timestamp类型,我可以用它创建一个Post结构。

Is there something obvious that I am doing wrong here? 有什么明显的东西我在这里做错了吗?

As an aside, I am pretty new to Rust and Diesel uses a fair bit of code-generation, so it's easy to get lost, but I thought this should be a straightforward thing to accomplish. 顺便说一下,我对Rust和Diesel很新,使用了相当多的代码生成,因此很容易迷失,但我认为这应该是一个简单易行的事情。


Edit : 编辑

I used timestamp with time zone to create the table, and it looks like that may not be supported yet : 我使用timestamp with time zone来创建表,看起来可能还不支持

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

Edit 2: 编辑2:

I changed models.rs to look like the following and got rid of the error about Timestamp being undefined. 我改变了models.rs看起来像下面的那样,摆脱了关于Timestamp未定义的错误。 I also realized that I needed #[derive(Queryable)] above each of the structs to be derived. 我也意识到我需要在每个结构上面#[derive(Queryable)] The following compiles fine, but the previous errors with Timestamptz remain: 以下编译正常,但以前的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
}

All of the types in diesel::sql_types are markers to represent various SQL datatypes for your schema. diesel::sql_types中的所有类型都是表示架构的各种SQL数据类型的标记。 They should never be used in your own structs. 它们永远不应该用在你自己的结构中。 What you need is a type which implements diesel::deserialize::FromSql<diesel::sql_types::Timestamp, diesel::pg::Pg> (docs: FromSql , Timestamp , Pg ). 你需要的是一个实现diesel::deserialize::FromSql<diesel::sql_types::Timestamp, diesel::pg::Pg> (docs: FromSqlTimestampPg )的类型。 There are two types which implement that trait. 有两种类型可以实现这种特性。

The first is std::time::SystemTime which doesn't require additional dependencies, but doesn't have a ton of capabilities. 第一个是std::time::SystemTime ,它不需要额外的依赖关系,但没有大量的功能。

The second is chrono::NaiveDateTime . 第二个是chrono::NaiveDateTime This is probably the type you want. 这可能是你想要的类型。 In order to use it, you'll need to add chrono to your dependencies, and change the diesel line in Cargo.toml to include the chrono feature, so it'll look something like diesel = { version = "0.7.0", features = ["postgres", "chrono"] } 为了使用它,你需要添加chrono在Cargo.toml到你的依赖,改变柴油线,包括计时功能,所以它会看起来像diesel = { version = "0.7.0", features = ["postgres", "chrono"] }

(Technically there's a third type, which is diesel::data_types::PgTimestamp but that's almost certainly not what you want, as that struct is just the literal representation of timestamp in the database so other types don't have to worry about raw bytes) (从技术上讲,有第三种类型,即diesel::data_types::PgTimestamp但几乎肯定不是你想要的,因为该结构只是数据库中时间戳的文字表示,因此其他类型不必担心原始字节)

please check datatype from ui 请检查ui的数据类型

"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 " “src / models.rs:16:18:16:27帮助:你可以将它导入范围: use diesel::types::Timestamp; .src / models.rs:17:18:17:27错误:输入名称Timestamp未定义或未在范围内[E0412] src / models.rs:17 pub updated:Timestamp“

maybe timestamp is not define word. 也许时间戳不是定义单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM