title: Timestamp in Rust's Diesel Library with Postgres
body: <p>I have been taking a look at Rust's <a href="https://github.com/diesel-rs/diesel" rel="noreferrer">Diesel ORM</a> today by following along on <a href="http://diesel.rs/guides/getting-started/" rel="noreferrer">this walk-through</a>, and I can't get a <code>Timestamp</code> to work. </p>
  
  <p><strong>Cargo.toml</strong></p>
  
  <pre><code>[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"
  </code></pre>
  
  <p><strong>models.rs</strong></p>
  
  <pre><code>#[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
  }
  </code></pre>
  
  <p>(I read that there's a <code>diesel::types::Timestamp</code> type)</p>
  
  <p><strong>lib.rs</strong></p>
  
  <pre><code>#![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() -&gt; PgConnection {
      dotenv().ok();
  
      let database_url = env::var("DATABASE_URL").
          expect("DATABASE_URL must be set");
      PgConnection::establish(&amp;database_url).
          expect(&amp;format!("Error connecting to {}", database_url))
  }
  </code></pre>
  
  <p>But these are the errors I get when I try to use it:</p>
  
  <pre class="lang-none prettyprint-override"><code>&lt;diesel macros&gt;:5:1: 5:71 note: in this expansion of table_body! (defined in &lt;diesel macros&gt;)
  src/schema.rs:1:1: 1:40 note: in this expansion of table! (defined in &lt;diesel macros&gt;)
  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)]
  
  ...
  
  &lt;diesel macros&gt;:38:1: 38:47 note: in this expansion of column! (defined in &lt;diesel macros&gt;)
  &lt;diesel macros&gt;:5:1: 5:71 note: in this expansion of table_body! (defined in &lt;diesel macros&gt;)
  src/schema.rs:1:1: 1:40 note: in this expansion of table! (defined in &lt;diesel macros&gt;)
  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
                                ^~~~~~~~~
  </code></pre>
  
  <p>It looks like the first error, <code>Timestamptz</code> is a result of <code>infer_schema</code> not knowing how to interpret that Postgresql type, which is already in the table. As for the second, I thought perhaps if explicitly imported that <code>Timestamp</code> type, I could create a <code>Post</code> struct with it.</p>
  
  <p>Is there something obvious that I am doing wrong here?</p>
  
  <p>As an aside, I am pretty new to Rust and Diesel uses a fair bit of code-generation, so it's easy to get lost, but I thought this should be a straightforward thing to accomplish.</p>
  
  <hr>
  
  <p><strong>Edit</strong>: </p>
  
  <p>I used <code>timestamp with time zone</code> to create the table, and it looks like that <a href="https://github.com/diesel-rs/diesel/issues/106" rel="noreferrer">may not be supported yet</a>:</p>
  
  <pre><code>CREATE TABLE post (
      ...
      created timestamp with time zone NOT NULL,
      updated timestamp with time zone
  )
  </code></pre>
  
  <hr>
  
  <p><strong>Edit 2:</strong> </p>
  
  <p>I changed <strong>models.rs</strong> to look like the following and got rid of the error about <code>Timestamp</code> being undefined. I also realized that I needed <code>#[derive(Queryable)]</code> above each of the structs to be derived. The following compiles fine, but the previous errors with <code>Timestamptz</code> remain:</p>
  
  <pre><code>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
  }
  </code></pre>