Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upJoint pain #631
Comments
This comment has been minimized.
|
You have to derive the trait |
This comment has been minimized.
Boscop
commented
Feb 7, 2017
•
|
@golddranks I have: #[derive(Queryable, new, Debug, Associations)]
#[belongs_to(User)]
#[belongs_to(Post)]
#[derive(Insertable)]
#[table_name="likes"]
pub struct Like {
pub user_id: i64,
pub post_id: i64,
}
...
#[derive(Queryable, Identifiable, Clone, Associations, Debug)]
#[belongs_to(User)]
// #[belongs_to(Post, foreign_key="parent")]
pub struct Post {
pub id: i64,
pub user_id: i64,
pub parent: Option<i64>,
...
}If I add error[E0223]: ambiguous associated type
|
130 | #[derive(Queryable, Identifiable, Clone, Associations, Debug)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ambiguous associated type
|
= note: specify the type using the syntax `<models::like::Like as Trait>::table`
= note: this error originates in a macro outside of the current crateWhat now? |
This comment has been minimized.
|
I'm not 100% sure about the problem because the error message isn't too descriptive, but at least |
This comment has been minimized.
Boscop
commented
Feb 7, 2017
•
|
@golddranks Thanks, I changed it to error[E0308]: mismatched types
|
194 | likes::table.inner_join(posts::table).filter(user_id.eq(uid)).count()
| ^^^^^^^^^^^^ expected struct `models::schema::__diesel_infer_schema::infer_users::users::table`, found struct `models::schema::__diesel_infer_schema::infer_posts::posts::table`
|
= note: expected type `models::schema::__diesel_infer_schema::infer_users::users::table`
found type `models::schema::__diesel_infer_schema::infer_posts::posts::table` |
This comment has been minimized.
|
You might want to add Also, the error message sounds like it's infering the types wrong. Is there anything external that might make it expect users instead of posts? What is the type of |
This comment has been minimized.
Boscop
commented
Feb 7, 2017
|
Ah, nevermind, I had changed this: #[belongs_to(User)]
#[belongs_to(Post)]to this: #[belongs_to(User, Post)]because I assumed it would work, just like: #[derive(Queryable, Insertable, new, Debug, Associations)]Now that I changed it back to the former version, it compiles, yay! |
This comment has been minimized.
|
I'm only reading this via email. The problem was adding the belongs_to attribute twice?
Does the documentation mention this? (If not, PR welcome!)
… Am 07.02.2017 um 12:21 schrieb Boscop ***@***.***>:
Ah, nevermind, I had changed this:
#[belongs_to(User)]
#[belongs_to(Post)]
to this:
#[belongs_to(User, Post)]
because I assumed it would work, just like:
#[derive(Queryable, Insertable, new, Debug, Associations)]
Now that I changed it back to the former version, it compiles, yay!
Thanks!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
This comment has been minimized.
Boscop
commented
Feb 7, 2017
|
@killercup The issue (at the end) was that I thought one could specify multiple structs that a struct belongs to in one #[belongs_to()] attribute, because it works with #[derive()].. Btw, now this works: pub fn get_likes_count(&self) -> Result<i64, error::MyError> {
use diesel::prelude::*;
use models::schema::likes;
use models::schema::posts;
use models::schema::posts::dsl::*;
likes::table.inner_join(posts::table).filter(user_id.eq(self.id)).count()
.get_result(&*database::connection().get().unwrap()).map_err(|e| e.into())
}But this doesn't: pub fn get_liked_posts(&self) -> Result<Vec<models::post::Post>, error::MyError> {
use diesel::prelude::*;
use models::schema::likes;
use models::schema::posts;
use models::schema::posts::dsl::*;
likes::table.inner_join(posts::table).filter(user_id.eq(self.id))
.get_results(&*database::connection().get().unwrap()).map_err(|e| e.into())
}
Because the output has the columns of likes and posts, but it should only have the columns of posts. |
This comment has been minimized.
Boscop
commented
Feb 7, 2017
|
I also tried splitting it up into two queries like this: pub fn get_liked_posts(&self) -> Result<Vec<models::post::Post>, error::MyError> {
use diesel::prelude::*;
use models::schema::likes;
use models::schema::posts;
use models::schema::posts::dsl::*;
let conn = &*database::connection().get().unwrap();
let likes = models::like::Like::belonging_to(self).load(conn)?;
models::post::Post::belonging_to(&likes).order(created_at.desc()).load(conn)?
}But I get:
But I have: #[derive(Queryable, Insertable, Associations, new, Debug)]
#[belongs_to(User)]
#[belongs_to(Post)]
#[table_name="likes"]
pub struct Like {
pub user_id: i64,
pub post_id: i64,
}
#[derive(Queryable, Identifiable, Associations, Debug, Clone)]
#[has_many(likes)]
pub struct User {
pub id: i64,
...
}So why doesn't |
Boscop
changed the title from
Joins don't work like the doc says
to
Joint pain
Feb 7, 2017
This comment has been minimized.
|
You need to add |
This comment has been minimized.
Boscop
commented
Feb 7, 2017
|
@sgrif Thanks! #[derive(Queryable, Identifiable, Insertable, Associations, new, Debug)]
#[belongs_to(User)]
#[belongs_to(Post)]
#[primary_key(user_id, post_id)]
#[table_name="likes"]
pub struct Like {
....
}
#[derive(Queryable, Identifiable, Associations, Clone, Debug)]
#[belongs_to(User)]
#[has_many(likes)]
pub struct Post {
...
}but I still get errors: pub fn get_liked_posts(&self) -> Result<Vec<models::post::Post>, error::MyError> {
use diesel::prelude::*;
use models::schema::likes;
use models::schema::posts;
use models::schema::posts::dsl::*;
use models::like::Like;
use models::post::Post;
let conn = &*database::connection().get().unwrap();
let likes: Vec<Like> = Like::belonging_to(self).load(conn).map_err(|e| e.into())?;
Post::belonging_to(&likes).order(created_at.desc()).load(conn).map_err(|e| e.into())
}
This version also doesn't work; how can I get rid of the columns of pub fn get_liked_posts(&self) -> Result<Vec<models::post::Post>, error::MyError> {
use diesel::prelude::*;
use models::schema::likes;
use models::schema::posts;
use models::schema::posts::dsl::*;
use models::like::Like;
use models::post::Post;
let conn = &*database::connection().get().unwrap();
likes::table.inner_join(posts::table).filter(user_id.eq(self.id))
.get_results(conn).map_err(|e| e.into())
}
|
This comment has been minimized.
Boscop
commented
Feb 7, 2017
|
This seems to work: pub fn get_liked_posts(&self) -> Result<Vec<models::post::Post>, error::MyError> {
use diesel::prelude::*;
use models::schema::likes;
use models::schema::likes::dsl::user_id;
use models::schema::posts;
use models::schema::posts::dsl::created_at;
use models::like::Like;
let conn = &*database::connection().get().unwrap();
Ok(posts::table.inner_join(likes::table).filter(user_id.eq(self.id)).order(created_at.desc()).get_results(conn)?.into_iter().unzip::<_, _, _, Vec<Like>>().0)
}But with get_results() I'm getting a Vec<(Post, Like)>, but I only need the Vec so I unzip it and throw away the Vec, which is wasteful.. |
This comment has been minimized.
Boscop
commented
Feb 7, 2017
•
|
Ah, I found a way that works: pub fn get_liked_posts(&self) -> Result<Vec<models::post::Post>, error::MyError> {
use diesel::prelude::*;
use models::schema::likes;
use models::schema::likes::dsl::user_id;
use models::schema::posts;
use models::schema::posts::dsl::created_at;
let conn = &*database::connection().get().unwrap();
Ok(posts::table.inner_join(likes::table).filter(user_id.eq(self.id))
.select(posts::all_columns).order(created_at.desc()).get_results(conn)?)
}Is this the best way to do it? |
This comment has been minimized.
|
Yes, that looks fine to me. |
sgrif
closed this
Feb 7, 2017
This comment has been minimized.
Boscop
commented
Feb 7, 2017
|
Thanks! |
This comment has been minimized.
mckinley-olsen-oc
commented
May 9, 2017
|
I ran into the issue in the first post, where diesel::query_source::InnerJoinSource has no method named load My issue was that I had not specified the |
Boscop commentedFeb 7, 2017
•
edited
I want to express this query efficiently with diesel:
I tried:
But I get:
Then I tried this:
But I get:
Even though the doc has this example:
What is the correct way to express the above SQL query in diesel?