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

Associations or joinable! probleams #1747

Closed
fundon opened this Issue Jun 1, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@fundon

fundon commented Jun 1, 2018

Setup

Versions

  • Rust: 1.26.1
  • Diesel: 1.3.1
  • Database: postgres 10.4
  • Operating System macOS 10.13.4

Feature Flags

  • diesel:
diesel = { version = "1.3", default-features = false, features = ["chrono", "postgres", "r2d2", "serde_json"] }

Problem Description

up.sql

CREATE TABLE leagues (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255) NOT NULL
};

CREATE TABLE IF NOT EXISTS teams (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255) UNIQUE NOT NULL,
  league_id INTEGER NOT NULL REFERENCES leagues (id)
};

CREATE TABLE IF NOT EXISTS matches (
  id SERIAL PRIMARY KEY,
  home_id INTEGER NOT NULL REFERENCES teams (id),
  away_id INTEGER NOT NULL REFERENCES teams (id),
  league_id INTEGER NOT NULL REFERENCES leagues (id)
};

schema.rs

table! {
    leagues (id) {
        id -> Int4,
        name -> Varchar,
    }
}

table! {
    matches (id) {
        id -> Int4,
        home_id -> Int4,
        away_id -> Int4,
        league_id -> Int4,
    }
}

table! {
    teams (id) {
        id -> Int4,
        name -> Varchar,
        league_id -> Int4,
    }
}

joinable!(matches -> leagues (league_id));
joinable!(teams -> leagues (league_id));

allow_tables_to_appear_in_same_query!(
    leagues,
    matches,
    teams,
);

What are you trying to accomplish?

not found

joinable!(matches -> teams (home_id));
joinable!(matches -> teams (away_id));

then, I add them to the schema.rs

What is the expected output?

What is the actual output?

Are you seeing any additional errors?

error[E0119]: conflicting implementations of trait `diesel::JoinTo<models::schema::teams::table>` for type `models::schema::matches::table`:
  --> src/models/schema.rs:73:1
   |
72 | joinable!(matches -> teams(home_id));
   | ------------------------------------- first implementation here
73 | joinable!(matches -> teams(away_id));
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `models::schema::matches::table`
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0119]: conflicting implementations of trait `diesel::JoinTo<models::schema::matches::table>` for type `models::schema::teams::table`:
  --> src/models/schema.rs:73:1
   |
72 | joinable!(matches -> teams(home_id));
   | ------------------------------------- first implementation here
73 | joinable!(matches -> teams(away_id));
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `models::schema::teams::table`
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0119`.
error: Could not compile `server`.

To learn more, run the command again with --verbose.

Steps to reproduce

Checklist

  • I have already looked over the issue tracker for similar issues.
  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
@sgrif

This comment has been minimized.

Member

sgrif commented Jun 1, 2018

Quoting the docs for joinable!

Allow two tables to be referenced in a join query without providing an explicit ON clause.

The only affect this macro has is decides what ON clause is used when you do left_table.inner_join(right_table). We can't have two implementations of that for the same two tables, the compiler doesn't know what you meant. You will need to provide the on clause explicitly.

@sgrif sgrif closed this Jun 1, 2018

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