New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple foreign keys with #[belongs_to], conflicting implementations of trait #616

Closed
Boscop opened this Issue Feb 4, 2017 · 7 comments

Comments

Projects
None yet
3 participants
@Boscop

Boscop commented Feb 4, 2017

I have a table posts, and every post has an author (user_id) and a possible parent post that it replies to:

#[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>,
	// ...
}

But I get:

error[E0119]: conflicting implementations of trait diesel::SelectableExpression<diesel::query_source::InnerJoinSource<models::schema::__diesel_infer_schema::infer_posts::posts::table, models::schema::__diesel_infer_schema::infer_posts::posts::table>, diesel::types::BigInt> for type models::schema::__diesel_infer_schema::infer_posts::posts::columns::id:

What's the correct way to declare multiple foreign keys for this kind of usecase?

@Eijebong

This comment has been minimized.

Member

Eijebong commented Feb 4, 2017

I don't think you can join more than 2 tables for now, have a look at #89

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 4, 2017

@Eijebong You can't join to more than two tables in a single query, but having multiple associations on a single model is definitely supported. This is a bug.

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 4, 2017

Ah, I see what's going on now. The issue isn't that there are multiple associations, it's that this is a self-referential association.

@Boscop

This comment has been minimized.

Boscop commented Feb 8, 2017

Yeah, posts can have a parent post that they reply to.
Will it be possible in the future to write Post::belonging_to(&post) for a self-referential association like this?

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 8, 2017

If by "Will it be possible in the future" you mean "is this a thing that I think is reasonable to do and want to support eventually?", then the answer is yes. If you mean "is this a thing you are actively working on and have a concrete timeline for?" then the answer is no

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 8, 2017

As I was writing that I realized this is literally just an if statement in codegen. I'm 99% certain the only problem with it is the invocations of select_column_workaround!. This should be trivial to support.

@sgrif sgrif added the accepted label Feb 8, 2017

@sgrif sgrif added this to the 0.11 milestone Feb 8, 2017

@Boscop

This comment has been minimized.

Boscop commented Feb 8, 2017

Yay!

sgrif added a commit that referenced this issue Feb 15, 2017

Don't generate join impls on self referencing associations
We don't support self-referencing joins right now, but that doesn't
mean that we can't support the rest of the associations API.

Fixes #616.

sgrif added a commit that referenced this issue Feb 15, 2017

Don't generate join impls on self referencing associations
We don't support self-referencing joins right now, but that doesn't
mean that we can't support the rest of the associations API.

Fixes #616.

@sgrif sgrif closed this in #710 Feb 15, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment