今天我一直在关注Rust's 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))
}
但当我try 使用它时,我会遇到以下错误:
<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
struct .
有没有什么明显的事情表明我做错了?
顺便说一句,我对Rust 和柴油机使用了相当多的代码生成,所以很容易丢失,但我认为这应该是一个简单的事情来完成.
Edit:
我用timestamp with time zone
创建了这个表,它看起来像may not be supported yet:
CREATE TABLE post (
...
created timestamp with time zone NOT NULL,
updated timestamp with time zone
)
Edit 2:
我将models.rs改为如下所示,并消除了Timestamp
未定义的错误.我还意识到,我需要在每个 struct 之上派生#[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
}