From a60b11925c5d020a31b027adfa17ac453acaa141 Mon Sep 17 00:00:00 2001 From: David Westgate Date: Sun, 2 Jun 2024 19:01:54 -0700 Subject: [PATCH] fix answers api --- migrations/0_init_schema.sql | 1 + src/pg_store.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/migrations/0_init_schema.sql b/migrations/0_init_schema.sql index 34c82a7..dd419d6 100644 --- a/migrations/0_init_schema.sql +++ b/migrations/0_init_schema.sql @@ -24,5 +24,6 @@ CREATE TABLE IF NOT EXISTS question_tag ( CREATE TABLE IF NOT EXISTS answers ( id SERIAL PRIMARY KEY, question_id INT REFERENCES questions(id), + FOREIGN KEY (question_id) REFERENCES questions(id), content TEXT NOT NULL ); diff --git a/src/pg_store.rs b/src/pg_store.rs index eab1deb..6e5705b 100644 --- a/src/pg_store.rs +++ b/src/pg_store.rs @@ -394,12 +394,12 @@ impl Store { // Add an answer entity pub async fn add_answer(&mut self, new_answer: NewAnswer) -> Result { - let result = - sqlx::query("INSERT INTO answers VALUES ($1,$2) RETURNING id, content, question_id") - .bind(new_answer.content) - .bind(new_answer.question_id.to_string()) - .fetch_one(&self.connection) - .await; + let query = "INSERT INTO answers (content, question_id) VALUES ($1,$2) RETURNING id, content, question_id"; + let result = sqlx::query(query) + .bind(new_answer.content) + .bind(i32::from(new_answer.question_id)) + .fetch_one(&self.connection) + .await; match result { Ok(pg_row) => Ok(Answer::new( Store::id_to_u8(&pg_row, "id"),