Macro diesel::joinable [] [src]

macro_rules! joinable {
    ($($child:ident)::* -> $($parent:ident)::* ($source:ident)) => { ... };
}

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

The generated ON clause will always join to the primary key of the parent table. This macro removes the need to call [.on] explicitly, you will still need to invoke [allow_tables_to_appear_in_same_query!] for these two tables to be able to use the resulting query, unless you are using infer_schema! or diesel print-schema which will generate it for you.

If you are using infer_schema! or diesel print-schema, an invocation of this macro will be generated for every foreign key in your database unless one of the following is true:

Example

use schema::*;

joinable!(posts -> users (user_id));
allow_tables_to_appear_in_same_query!(posts, users);

let implicit_on_clause = users::table.inner_join(posts::table);
let implicit_on_clause_sql = diesel::debug_query::<DB, _>(&implicit_on_clause).to_string();

let explicit_on_clause = users::table
    .inner_join(posts::table.on(posts::user_id.eq(users::id)));
let explicit_on_clause_sql = diesel::debug_query::<DB, _>(&explicit_on_clause).to_string();

assert_eq!(implicit_on_clause_sql, explicit_on_clause_sql);