我今天一直在关注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.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
不知道如何解释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
}
错误仍然存在:
/football/players/someplayer
答案 0 :(得分:20)
diesel::sql_types
中的所有类型都是表示架构的各种SQL数据类型的标记。它们永远不应该用在你自己的结构中。您需要的是一种实现diesel::deserialize::FromSql<diesel::sql_types::Timestamp, diesel::pg::Pg>
的类型(文档:FromSql
,Timestamp
,Pg
)。有两种类型可以实现这种特性。
第一个是std::time::SystemTime
,它不需要额外的依赖关系,但不具备大量功能。
第二个是chrono::NaiveDateTime
。这可能是你想要的类型。为了使用它,您需要将chrono
添加到您的依赖项中,并更改Cargo.toml中的柴油线以包含计时功能,因此它看起来像{{1 }}
(从技术上来说,第三种类型是diesel::data_types::PgTimestamp
,但这几乎肯定不是你想要的,因为该结构只是数据库中时间戳的文字表示,所以其他类型#39; t不得不担心原始字节)
答案 1 :(得分:0)
请检查ui的数据类型
&#34; 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更新:时间戳&#34;
也许时间戳不是定义单词。