Skip to content
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 joins between two tables are not possible #332

Closed
weiznich opened this issue May 18, 2016 · 2 comments
Closed

Multiple joins between two tables are not possible #332

weiznich opened this issue May 18, 2016 · 2 comments

Comments

@weiznich
Copy link
Member

Given the following two tables:

CREATE TABLE points(
        id BIGSERIAL PRIMARY KEY,
        x DOUBLE PRECISION NOT NULL,
        y DOUBLE PRECISION NOT NULL);

and

CREATE TABLE boxes(
        id BIGSERIAL PRIMARY KEY,
        o BIGINT REFERENCES points(id) NOT NULL,
        u BIGINT REFERENCES points(id) NOT NULL);

I want both relations to be joinable.

infer_table_from_schema!(dotenv!("DATABASE_URL"), "points");
infer_table_from_schema!(dotenv!("DATABASE_URL"), "boxes");

joinable!(boxes -> points (o));
joinable!(boxes -> points (u));

This is currently not possible because the joinable macro is generating a conflicting implementation of the trait JoinTo.

error: conflicting implementations of trait `diesel::JoinTo<points::table, _>` for type `boxes::table`: [--explain E0119]
 --> <diesel macros>:4:1
4 |> impl < JoinType > $ crate :: JoinTo < $ right_table , JoinType > for $
  |> ^

@bpbp-boop
Copy link

I just ran into this

table! {
    matches (id) {
        id -> Integer,
        date -> Nullable<Date>,
        time -> Nullable<Time>,
        home_team_id -> Nullable<Integer>,
        away_team_id -> Nullable<Integer>,
    }
}

table! {
    teams (id) {
        id -> Integer,
        name -> Text,
    }
}

joinable!(matches -> teams (home_team_id));
joinable!(matches -> teams (away_team_id));

Have any workaround surfaced since this was opened?

@sgrif
Copy link
Member

sgrif commented Oct 10, 2017

This is the expected behavior. Otherwise we have no clue what you mean when you say matches.inner_join(teams). You can use the .on method to specify an explicit on clause.

@sgrif sgrif closed this as completed Oct 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants