#Use JSON extractor with diesel

1 messages · Page 1 of 1 (latest)

silent trail
#

Hello!
I'm currently trying to use JSON extractor and inserting the values into the database using diesel.
Is there a way to do it? I would like to use Insertable diesel's feature but it seems not to work.
This is how I am doing it:

#[post("/create")]
pub(crate) async fn create_user(
    pool: web::Data<database::ConnectionPool>,
    info: web::Json<User>,
) -> String {
    diesel::insert_into(users)
        .values(info)
        .execute(&mut pool.get().await?)
        .await;

    format!("User created")
}

Struct:

#[derive(Serialize, Deserialize, Queryable, Selectable, Insertable)]
#[diesel(table_name = crate::schema::users)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub(crate) struct User {
    username: String,
    online: bool,
    user_group: UserGroupEnum,
}
#

This is the error I am getting:

error[E0277]: the trait bound `actix_web::web::Json<User>: diesel::Insertable<table>` is not satisfied
   --> src\controllers\user.rs:25:17
    |
25  |         .values(info)
    |          ------ ^^^^ the trait `diesel::Insertable<table>` is not implemented for `actix_web::web::Json<User>`
    |          |
    |          required by a bound introduced by this call
    |
    = help: the following other types implement trait `diesel::Insertable<T>`:
              <Box<[T; N]> as diesel::Insertable<Tab>>
              <User as diesel::Insertable<table>>
              <table as diesel::Insertable<T>>
              <diesel::insertable::private::InsertableOptionHelper<T, ColumnInsertValue<Col, Expr>> as diesel::Insertable<Tab>>
              <diesel::insertable::private::InsertableOptionHelper<__T, (ST0,)> as diesel::Insertable<Tab>>
              <diesel::insertable::private::InsertableOptionHelper<__T, (ST0, ST1)> as diesel::Insertable<Tab>>
              <diesel::insertable::private::InsertableOptionHelper<__T, (ST0, ST1, ST2)> as diesel::Insertable<Tab>>
              <diesel::insertable::private::InsertableOptionHelper<__T, (ST0, ST1, ST2, ST3)> as diesel::Insertable<Tab>>
            and 116 others
note: required by a bound in `IncompleteInsertStatement::<T, Op>::values`
   --> C:\Users\Ivan\.cargo\registry\src\index.crates.io-6f17d22bba15001f\diesel-2.1.3\src\query_builder\insert_statement\mod.rs:114:12
    |
112 |     pub fn values<U>(self, records: U) -> InsertStatement<T, U::Values, Op>
    |            ------ required by a bound in this associated function
113 |     where
114 |         U: Insertable<T>,
    |            ^^^^^^^^^^^^^ required by this bound in `IncompleteInsertStatement::<T, Op>::values`