refactor(db): Establish Post->Thread association
This makes it possible to query posts by thread via Diesel.
This commit is contained in:
parent
6e56f8e729
commit
316036b0a8
5 changed files with 15 additions and 7 deletions
1
migrations/2018-04-08-161017_joinable_posts/down.sql
Normal file
1
migrations/2018-04-08-161017_joinable_posts/down.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
ALTER TABLE posts RENAME COLUMN thread_id TO thread;
|
||||||
1
migrations/2018-04-08-161017_joinable_posts/up.sql
Normal file
1
migrations/2018-04-08-161017_joinable_posts/up.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
ALTER TABLE posts RENAME COLUMN thread TO thread_id;
|
||||||
|
|
@ -47,11 +47,15 @@ impl Handler<GetThread> for DbExecutor {
|
||||||
|
|
||||||
fn handle(&mut self, msg: GetThread, _: &mut Self::Context) -> Self::Result {
|
fn handle(&mut self, msg: GetThread, _: &mut Self::Context) -> Self::Result {
|
||||||
use schema::threads::dsl::*;
|
use schema::threads::dsl::*;
|
||||||
|
|
||||||
let conn = self.0.get().unwrap();
|
let conn = self.0.get().unwrap();
|
||||||
let result: Thread = threads
|
let thread_result: Thread = threads
|
||||||
.find(msg.0).first(&conn)
|
.find(msg.0).first(&conn)
|
||||||
.expect("Error loading thread");
|
.expect("Error loading thread");
|
||||||
|
|
||||||
Ok((result, vec![]))
|
let post_list = Post::belonging_to(&thread_result)
|
||||||
|
.load::<Post>(&conn).expect("Error loading posts for thread");
|
||||||
|
|
||||||
|
Ok((thread_result, post_list))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use chrono::prelude::{DateTime, Utc};
|
use chrono::prelude::{DateTime, Utc};
|
||||||
|
use schema::{threads, posts};
|
||||||
|
|
||||||
#[derive(Queryable, Serialize)]
|
#[derive(Identifiable, Queryable, Serialize)]
|
||||||
pub struct Thread {
|
pub struct Thread {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
|
@ -8,10 +9,11 @@ pub struct Thread {
|
||||||
pub posted: DateTime<Utc>,
|
pub posted: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Serialize)]
|
#[derive(Identifiable, Queryable, Serialize, Associations)]
|
||||||
|
#[belongs_to(Thread)]
|
||||||
pub struct Post {
|
pub struct Post {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub thread: i32,
|
pub thread_id: i32,
|
||||||
pub body: String,
|
pub body: String,
|
||||||
pub posted: DateTime<Utc>,
|
pub posted: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
table! {
|
table! {
|
||||||
posts (id) {
|
posts (id) {
|
||||||
id -> Int4,
|
id -> Int4,
|
||||||
thread -> Int4,
|
thread_id -> Int4,
|
||||||
body -> Text,
|
body -> Text,
|
||||||
posted -> Timestamptz,
|
posted -> Timestamptz,
|
||||||
}
|
}
|
||||||
|
|
@ -16,7 +16,7 @@ table! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
joinable!(posts -> threads (thread));
|
joinable!(posts -> threads (thread_id));
|
||||||
|
|
||||||
allow_tables_to_appear_in_same_query!(
|
allow_tables_to_appear_in_same_query!(
|
||||||
posts,
|
posts,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue