Problem with sharing same data struct with Diesel and Actix-Web

I created a method to accept HTTP POST with parameters:

#[post("/candidate/signup")]
pub fn candidate_signup(params: web::Form<Candidate>) -> impl Future<Item=HttpResponse, Error=Error> {
    println!("candidate_signup 0");
    candidates::signup(params.into_inner());
    future::ok(HttpResponse::Ok().finish())
}

and the Candidate data struct - same as the one use to model the database table, ie. in models.rs - is as follows:

#[derive(Queryable, Serialize, Deserialize)]
pub struct Candidate {
    pub id: i64,
    pub username: String,
    pub last_name: String,
    pub first_name: String,
}

That means both Actix-Web and Diesel are referring to the same data struct.

The "id" (i64) will never be passed from the web form, but it is a non-null field in the database. The problem is that Actix-Web will reject the request at the door if "id" is not part of the parameters (I tested with CURL). Actix-Web will only accept it if all the parameters are filled.

What is the solution to this problem, or it is just a bad idea to share the same data struct?

You don't want them to share the same struct. It's better to create a new struct without the id that derives Insertable.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.