我今天一直在按照这个演练查看 Rust 的Diesel ORM,但我无法工作。Timestamp
货运.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"
模型.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
类型)
库文件
#![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
创建表,看起来可能还不支持:
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
}