svenstaro / rust-web-boilerplate

Rust web template for modern web backend applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

QUESTION: 500 error, but rocket is saying everything is just fine when I try and insert a row

Jackbaude opened this issue · comments

commented

Here is my post request code, which was just modified from the boilerplate

#[post("/new-article", data = "<body>", format = "application/json")]
pub fn new_article(db: DbConn, body: Json<NewArticle>) -> Result<APIResponse, APIResponse>{
    let new_article = NewArticle {
        title: body.title.to_string(),
        description: body.description.to_string(),
        tag: body.tag.to_string()
    };
    println!("{}", &new_article);
    let insert_result = diesel::insert_into(articles::table)
        .values(&new_article)
        .get_result::<ArticleModel>(&*db);

    if let Err(diesel::result::Error::DatabaseError(
                   diesel::result::DatabaseErrorKind::UniqueViolation,
                   _,
               )) = insert_result
    {
        return Err(conflict().message("Article already exists."));
    }

    let user = insert_result?;
    Ok(created().data(json!(&user)))
}

The SQL table

CREATE TABLE articles
(
    "id"          SERIAL  NOT NULL PRIMARY KEY,
    "title"       TEXT    NOT NULL,
    "tag"         TEXT    NOT NULL,
    "description" TEXT    NOT NULL,
    "last_edited" TIMESTAMP DEFAULT current_timestamp NOT NULL,
    "edit_count"  INTEGER NOT NULL DEFAULT 0,
    "status"      BOOLEAN NOT NULL DEFAULT true,
    "publicized"  BOOLEAN NOT NULL DEFAULT false,
    "featured"    BOOLEAN NOT NULL DEFAULT false,
);
-- CreateIndex
CREATE UNIQUE INDEX articles.id_unique ON articles(id);

And my Models

#[derive(Debug, Serialize, Deserialize, Queryable, Identifiable, AsChangeset)]
#[table_name="articles"]
pub struct ArticleModel {
    pub id: i32,
    pub title: String,
    pub tag: String,
    pub description: String,
    pub last_edited: NaiveDateTime,
    pub edit_count: i32,
    pub status: bool,
    pub publicized: bool,
    pub featured: bool,
}

#[derive(Insertable, Debug, Deserialize)]
#[table_name="articles"]
pub struct NewArticle {
    pub title: String,
    pub description: String,
    pub tag: String,
}

Any help? I dont know where else to go

Is this solved? If so, what was the problem?

commented

Oops sorry I thought I closed this with a comment, it was a simple version issue with diesel.

Ok, cool, thanks for the update.