New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL: SystemTime does not implement diesel::types::Timestamp #874

Closed
iH8c0ff33 opened this Issue Apr 29, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@iH8c0ff33

iH8c0ff33 commented Apr 29, 2017

Here is my migration:

CREATE TABLE events (
  time TIMESTAMP PRIMARY KEY
)

The model:

use std::time::SystemTime;

#[derive(Queryable)]
pub struct Event {
    pub time: SystemTime
}

And some basic code:

fn main() {
    use time::schema::events::dsl::*;
    let connection = establish_connection();
    let results = events.select(time)
        .limit(5)
        .load::<Event>(&connection)
        .expect("Error loading events");

    println!("Displaying {} events", results.len());
    for post in results {
        println!("{}", post.time);
    }
}

If I try to compile the code above I get the following error, but in the documentation for diesel::types::Timestamp I see SystemTime should be a correct implementation.

error[E0277]: the trait bound `(std::time::SystemTime,): diesel::types::FromSqlRow<diesel::types::Timestamp, _>` is not satisfied
  --> src/bin/show_events.rs:13:10
   |
13 |         .load::<Event>(&connection)
   |          ^^^^ the trait `diesel::types::FromSqlRow<diesel::types::Timestamp, _>` is not implemented for `(std::time::SystemTime,)`
   |
   = help: the following implementations were found:
             <(A,) as diesel::types::FromSqlRow<(SA,), DB>>
   = note: required because of the requirements on the impl of `diesel::Queryable<diesel::types::Timestamp, _>` for `time::models::Event`

I'm not at all an experienced rust programmer and I'm using diesel for the first time, but I can't find anything wrong with that code, please tell me if I'm wrong. Thanks for all your help.

Environment details:

rust:
  stable-x86_64-apple-darwin (default)
  rustc 1.16.0 (30cf806ef 2017-03-10)
diesel:
  version = "0.12.0", features = ["postgres"]
@Eijebong

This comment has been minimized.

Member

Eijebong commented Apr 29, 2017

Here is the table! invocation

table! {
    events (time) {
        time -> Timestamp,
    }
}

I tried you code myself on rust nightly and it didn't compile either.
Removing the .select(time) make the code compile though.

I'm looking at the code right now to find an explanation ^^

@sgrif

This comment has been minimized.

Member

sgrif commented Apr 29, 2017

This is basically just a slightly confusing case that only applies when you have exactly 1 column. #[derive(Queryable)] wants all the fields as a tuple, since the overwhelmingly common case is to have more than one field. .select(time) creates a query with the type Timestamp, but your struct is expecting a query of type (Timestamp,). You can do .select((time,)) to get the desired result. It's less than ideal in this case, but I don't think there's a ton of value in special casing the single column queryable case as it's so rare.

@sgrif sgrif closed this Apr 29, 2017

@iH8c0ff33

This comment has been minimized.

iH8c0ff33 commented Apr 29, 2017

@sgrif
I'm really sorry for having been that dumb. Thanks for the time you spent helping me.

@sgrif

This comment has been minimized.

Member

sgrif commented Apr 30, 2017

Not dumb at all. It's an unfortunately confusing case and I'm always happy to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment