我今天一直在看看Rust的Diesel ORM跟随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.人生
#[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
创建表,它看起来像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
}
diesel::sql_types
中的所有类型都是表示模式的各种SQL数据类型的标记。它们永远不应该用在你自己的结构中。你需要的是一种实现diesel::deserialize::FromSql<diesel::sql_types::Timestamp, diesel::pg::Pg>
的类型(docs:FromSql
,Timestamp
,Pg
)。有两种类型可以实现这种特性。
第一个是std::time::SystemTime
,它不需要额外的依赖,但没有很多功能。
第二个是chrono::NaiveDateTime
。这可能是你想要的类型。为了使用它,你需要将chrono
添加到你的依赖项,并更改Cargo.toml中的柴油线以包含计时功能,所以它看起来像diesel = { version = "0.7.0", features = ["postgres", "chrono"] }
(从技术上讲,有第三种类型,即diesel::data_types::PgTimestamp
,但几乎肯定不是你想要的,因为该结构只是数据库中时间戳的文字表示,因此其他类型不必担心原始字节)
请检查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 pub更新:Timestamp“
也许时间戳不是定义单词。