Skip to content
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

the trait bound NaiveDateTime: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg> is not satisfied #2758

Closed
3 tasks done
tuanhuu162 opened this issue May 6, 2021 · 2 comments
Labels

Comments

@tuanhuu162
Copy link

Setup

Versions

  • Rust: rustc 1.51.0 (2fd73fabe 2021-03-23)
  • Diesel: 1.4.6
  • Database: postgresql
  • Operating System: Unbuntu 18.04

Feature Flags

  • diesel:

Problem Description

I am trying to write a module that can query and insert into tables in PostgreSQL. But when I use a nullable timestamp field, the compiler panic with an error:

error[E0277]: the trait bound `NaiveDateTime: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg>` is not satisfied
  --> src/database/user.rs:66:18
   |
66 |                 .get_result(&connection.get().unwrap())?;
   |                  ^^^^^^^^^^ the trait `FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg>` is not implemented for `NaiveDateTime`
   |
   = help: the following implementations were found:
             <NaiveDateTime as FromSql<diesel::sql_types::Timestamp, Pg>>
             <NaiveDateTime as FromSql<diesel::sql_types::Timestamptz, Pg>>
   = note: required because of the requirements on the impl of `Queryable<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg>` for `NaiveDateTime`
   = note: 2 redundant requirements hidden
   = note: required because of the requirements on the impl of `Queryable<(diesel::sql_types::Integer, diesel::sql_types::Uuid, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Nullable<diesel::sql_types::Timestamp>), Pg>` for `User`
   = note: required because of the requirements on the impl of `LoadQuery<PooledConnection<ConnectionManager<PgConnection>>, User>` for `InsertStatement<users::table, ValuesClause<(ColumnInsertValue<users::columns::email, diesel::expression::bound::Bound<diesel::sql_types::Text, std::string::String>>, ColumnInsertValue<users::columns::name, diesel::expression::bound::Bound<diesel::sql_types::Text, std::string::String>>, ColumnInsertValue<users::columns::hash, diesel::expression::bound::Bound<diesel::sql_types::Text, std::string::String>>), users::table>>`

I think the Nullable should be implemented here.

What are you trying to accomplish?

A lacked feature when using diesel.

What is the expected output?

That it should compile (or show me some other errors).

What is the actual output?

warning: unused manifest key: dependencies.serde.feature
   Compiling todolist v0.1.0 (/home/ubuntu/work/rust_space/todolist)
warning: unused import: `error::BlockingError`
 --> src/api/user_handle.rs:1:17
  |
1 | use actix_web::{error::BlockingError, web, HttpResponse, Responder};
  |                 ^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `crate::errors::ToDoError`
 --> src/api/user_handle.rs:7:5
  |
7 | use crate::errors::ToDoError;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused import: `Queryable`
 --> src/database/user.rs:7:23
  |
7 | use diesel::prelude::{Queryable, Insertable};
  |                       ^^^^^^^^^

error[E0277]: the trait bound `NaiveDateTime: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg>` is not satisfied
  --> src/database/user.rs:66:18
   |
66 |                 .get_result(&connection.get().unwrap())?;
   |                  ^^^^^^^^^^ the trait `FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg>` is not implemented for `NaiveDateTime`
   |
   = help: the following implementations were found:
             <NaiveDateTime as FromSql<diesel::sql_types::Timestamp, Pg>>
             <NaiveDateTime as FromSql<diesel::sql_types::Timestamptz, Pg>>
   = note: required because of the requirements on the impl of `Queryable<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg>` for `NaiveDateTime`
   = note: 2 redundant requirements hidden
   = note: required because of the requirements on the impl of `Queryable<(diesel::sql_types::Integer, diesel::sql_types::Uuid, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Nullable<diesel::sql_types::Timestamp>), Pg>` for `User`
   = note: required because of the requirements on the impl of `LoadQuery<PooledConnection<ConnectionManager<PgConnection>>, User>` for `InsertStatement<users::table, ValuesClause<(ColumnInsertValue<users::columns::email, diesel::expression::bound::Bound<diesel::sql_types::Text, std::string::String>>, ColumnInsertValue<users::columns::name, diesel::expression::bound::Bound<diesel::sql_types::Text, std::string::String>>, ColumnInsertValue<users::columns::hash, diesel::expression::bound::Bound<diesel::sql_types::Text, std::string::String>>), users::table>>`

error[E0277]: the trait bound `NaiveDateTime: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg>` is not satisfied
  --> src/database/user.rs:88:18
   |
88 |                 .load::<User>(&connection.get().unwrap())?;
   |                  ^^^^ the trait `FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg>` is not implemented for `NaiveDateTime`
   |
   = help: the following implementations were found:
             <NaiveDateTime as FromSql<diesel::sql_types::Timestamp, Pg>>
             <NaiveDateTime as FromSql<diesel::sql_types::Timestamptz, Pg>>
   = note: required because of the requirements on the impl of `Queryable<diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, Pg>` for `NaiveDateTime`
   = note: 2 redundant requirements hidden
   = note: required because of the requirements on the impl of `Queryable<(diesel::sql_types::Integer, diesel::sql_types::Uuid, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Nullable<diesel::sql_types::Timestamp>), Pg>` for `User`
   = note: required because of the requirements on the impl of `LoadQuery<_, User>` for `diesel::query_builder::SelectStatement<users::table, query_builder::select_clause::DefaultSelectClause, query_builder::distinct_clause::NoDistinctClause, query_builder::where_clause::WhereClause<diesel::expression::operators::Eq<users::columns::email, diesel::expression::bound::Bound<diesel::sql_types::Text, &std::string::String>>>>`

warning: unused import: `Insertable`
 --> src/database/user.rs:7:34
  |
7 | use diesel::prelude::{Queryable, Insertable};
  |                                  ^^^^^^^^^^

error: aborting due to 2 previous errors; 4 warnings emitted

For more information about this error, try `rustc --explain E0277`.
error: could not compile `todolist`

To learn more, run the command again with --verbose.

Are you seeing any additional errors?

No

Steps to reproduce

Run cargo run on my code base
I have the following lines in my src/lib.rs file in this order:

extern crate diesel;
// ...
use diesel::prelude::*;
// ...
mod schema;
mod models;

The diesel-generated src/schema.rs file looks like this:

table! {
    users (user_id) {
        user_id -> Int4,
        user_uuid -> Uuid,
        name -> Varchar,
        email -> Varchar,
        hash -> Varchar,
        created_at -> Nullable<Timestamp>,
    }
}

And the src/models.rs file looks like this:

#[derive(Debug, Deserialize, Serialize, Queryable, Insertable)]
#[table_name = "users"]
pub struct User {
    #[serde(skip)]
    pub user_id: i32,
    #[serde(skip)]
    pub user_uuid: Uuid,
    pub email: String,
    pub name: String,
    #[serde(skip)]
    pub hash: String,
    pub created_at: NaiveDateTime
}

Checklist

  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate
@tuanhuu162 tuanhuu162 added the bug label May 6, 2021
@weiznich
Copy link
Member

weiznich commented May 6, 2021

Closed duplicate of #2011 and #2434 and any other issue with an similar error message. See the documentation of the Nullable SQL type for compatible rust types.

@weiznich weiznich closed this as completed May 6, 2021
@sulaman0
Copy link

sulaman0 commented Oct 4, 2023

Hi @tuanhuu162 use this line in model.rs
pub created_at: Option<NaiveDateTime>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants