-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from mcasper/fix_foreign_key_inference
Fix foreign key and module inference for multi word structs
- Loading branch information
Showing
9 changed files
with
130 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use diesel::*; | ||
use schema::*; | ||
|
||
#[test] | ||
fn association_where_struct_name_doesnt_match_table_name() { | ||
#[derive(PartialEq, Eq, Debug, Clone, Queryable)] | ||
#[belongs_to(post)] | ||
#[table_name="comments"] | ||
struct OtherComment { | ||
id: i32, | ||
post_id: i32 | ||
} | ||
|
||
let connection = connection_with_sean_and_tess_in_users_table(); | ||
|
||
let sean = find_user_by_name("Sean", &connection); | ||
let post: Post = insert(&sean.new_post("Hello", None)).into(posts::table) | ||
.get_result(&connection).unwrap(); | ||
insert(&NewComment(post.id, "comment")).into(comments::table) | ||
.execute(&connection).unwrap(); | ||
|
||
let comment_text = OtherComment::belonging_to(&post).select(comments::text) | ||
.first::<String>(&connection); | ||
assert_eq!(Ok("comment".into()), comment_text); | ||
} | ||
|
||
|
||
#[test] | ||
fn association_where_parent_and_child_have_underscores() { | ||
#[derive(PartialEq, Eq, Debug, Clone, Queryable)] | ||
#[has_many(special_comments)] | ||
#[belongs_to(user)] | ||
struct SpecialPost { | ||
id: i32, | ||
user_id: i32, | ||
title: String | ||
} | ||
|
||
#[insertable_into(special_posts)] | ||
struct NewSpecialPost { | ||
user_id: i32, | ||
title: String | ||
} | ||
|
||
impl SpecialPost { | ||
fn new(user_id: i32, title: &str) -> NewSpecialPost { | ||
NewSpecialPost { | ||
user_id: user_id, | ||
title: title.to_owned() | ||
} | ||
} | ||
} | ||
|
||
#[derive(PartialEq, Eq, Debug, Clone, Queryable)] | ||
#[belongs_to(special_post)] | ||
struct SpecialComment { | ||
id: i32, | ||
special_post_id: i32, | ||
} | ||
|
||
impl SpecialComment { | ||
fn new(special_post_id: i32) -> NewSpecialComment { | ||
NewSpecialComment { | ||
special_post_id: special_post_id | ||
} | ||
} | ||
} | ||
|
||
#[insertable_into(special_comments)] | ||
struct NewSpecialComment { | ||
special_post_id: i32 | ||
} | ||
|
||
let connection = connection_with_sean_and_tess_in_users_table(); | ||
|
||
let sean = find_user_by_name("Sean", &connection); | ||
let new_post = SpecialPost::new(sean.id, "title"); | ||
let special_post: SpecialPost = insert(&new_post).into(special_posts::table) | ||
.get_result(&connection).unwrap(); | ||
let new_comment = SpecialComment::new(special_post.id); | ||
insert(&new_comment).into(special_comments::table) | ||
.execute(&connection).unwrap(); | ||
|
||
let comment: SpecialComment = SpecialComment::belonging_to(&special_post) | ||
.first(&connection).unwrap(); | ||
|
||
assert_eq!(special_post.id, comment.special_post_id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod schema; | ||
mod insert; | ||
mod annotations; | ||
mod deserialization; | ||
mod insert; | ||
mod schema; | ||
mod update; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
migrations/20160116104628_create_special_posts_and_special_comments/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP TABLE special_posts; | ||
DROP TABLE special_comments; |
10 changes: 10 additions & 0 deletions
10
migrations/20160116104628_create_special_posts_and_special_comments/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE special_posts ( | ||
id SERIAL PRIMARY KEY, | ||
user_id INTEGER NOT NULL, | ||
title VARCHAR NOT NULL | ||
); | ||
|
||
CREATE TABLE special_comments ( | ||
id SERIAL PRIMARY KEY, | ||
special_post_id INTEGER NOT NULL | ||
); |