带有 Postgres 的 Rust 柴油机库中的时间戳
Timestamp in Rust's Diesel Library with Postgres
我一直在查看 Rust 的 Diesel ORM today by following along on this 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))
}
但这些是我在尝试使用它时遇到的错误:
<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
不知道如何解释已经在 table 中的 Postgresql 类型。至于第二个,我想也许如果显式导入 Timestamp
类型,我可以用它创建一个 Post
结构。
有什么明显的地方我做错了吗?
顺便说一句,我对 Rust 很陌生,Diesel 使用了相当多的代码生成,所以很容易迷路,但我认为这应该是一件很容易完成的事情。
编辑:
我用 timestamp with time zone
创建了 table,它看起来像 may not be supported yet:
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
}
请检查 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 发布更新:时间戳“
可能时间戳不是定义词。
diesel::sql_types
are markers to represent various SQL datatypes for your schema. 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
中的所有类型)。有两种类型实现了该特征。
第一个是 std::time::SystemTime
,它不需要额外的依赖项,但没有很多功能。
第二个是 chrono::NaiveDateTime
. This is probably the type you want. In order to use it, you'll need to add chrono
到您的依赖项,并更改 Cargo.toml 中的柴油线以包含计时功能,因此它看起来像 diesel = { version = "0.7.0", features = ["postgres", "chrono"] }
(从技术上讲,还有第三种类型,即 diesel::data_types::PgTimestamp
但这几乎肯定不是您想要的,因为该结构只是数据库中时间戳的字面表示,因此其他类型不必担心关于原始字节)
我一直在查看 Rust 的 Diesel ORM today by following along on this 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))
}
但这些是我在尝试使用它时遇到的错误:
<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
不知道如何解释已经在 table 中的 Postgresql 类型。至于第二个,我想也许如果显式导入 Timestamp
类型,我可以用它创建一个 Post
结构。
有什么明显的地方我做错了吗?
顺便说一句,我对 Rust 很陌生,Diesel 使用了相当多的代码生成,所以很容易迷路,但我认为这应该是一件很容易完成的事情。
编辑:
我用 timestamp with time zone
创建了 table,它看起来像 may not be supported yet:
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
}
请检查 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 发布更新:时间戳“
可能时间戳不是定义词。
diesel::sql_types
are markers to represent various SQL datatypes for your schema. 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
中的所有类型)。有两种类型实现了该特征。
第一个是 std::time::SystemTime
,它不需要额外的依赖项,但没有很多功能。
第二个是 chrono::NaiveDateTime
. This is probably the type you want. In order to use it, you'll need to add chrono
到您的依赖项,并更改 Cargo.toml 中的柴油线以包含计时功能,因此它看起来像 diesel = { version = "0.7.0", features = ["postgres", "chrono"] }
(从技术上讲,还有第三种类型,即 diesel::data_types::PgTimestamp
但这几乎肯定不是您想要的,因为该结构只是数据库中时间戳的字面表示,因此其他类型不必担心关于原始字节)