allow_tables_to_appear_in_same_query

Macro allow_tables_to_appear_in_same_query 

Source
allow_tables_to_appear_in_same_query!() { /* proc-macro */ }
Expand description

Allow two or more tables which are otherwise unrelated to be used together in a query.

This macro must be invoked any time two tables need to appear in the same query either because they are being joined together, or because one appears in a subselect. When this macro is invoked with more than 2 tables, every combination of those tables will be allowed to appear together.

If you are using diesel print-schema, an invocation of this macro will be generated for you for all tables in your schema.

§Example

// This would be required to do `users.inner_join(posts.inner_join(comments))`
allow_tables_to_appear_in_same_query!(comments, posts, users);

table! {
    comments {
        id -> Integer,
        post_id -> Integer,
        body -> VarChar,
    }
}

table! {
   posts {
       id -> Integer,
       user_id -> Integer,
       title -> VarChar,
   }
}

table! {
    users {
       id -> Integer,
       name -> VarChar,
    }
}

When more than two tables are passed, the relevant code is generated for every combination of those tables. This code would be equivalent to the previous example.

allow_tables_to_appear_in_same_query!(comments, posts);
allow_tables_to_appear_in_same_query!(comments, users);
allow_tables_to_appear_in_same_query!(posts, users);

§Expanded Code

Expanded Code

§Simple example

§Input
allow_tables_to_appear_in_same_query! {
    users, posts, comments
}
§Expanded Code
Expanded code might use diesel internal API's and is only shown for educational purpose

The macro expands the input to the following Rust code:

impl ::diesel::query_source::TableNotEqual<posts::table> for users::table {}
impl ::diesel::query_source::TableNotEqual<users::table> for posts::table {}
impl ::diesel::query_source::TableNotEqual<comments::table> for users::table {}
impl ::diesel::query_source::TableNotEqual<users::table> for comments::table {}
impl ::diesel::query_source::TableNotEqual<comments::table> for posts::table {}
impl ::diesel::query_source::TableNotEqual<posts::table> for comments::table {}

§With paths

§Input
allow_tables_to_appear_in_same_query! {
    schema::users, schema::posts, comments
}
§Expanded Code
Expanded code might use diesel internal API's and is only shown for educational purpose

The macro expands the input to the following Rust code:

impl ::diesel::query_source::TableNotEqual<schema::posts::table>
for schema::users::table {}
impl ::diesel::query_source::TableNotEqual<schema::users::table>
for schema::posts::table {}
impl ::diesel::query_source::TableNotEqual<comments::table> for schema::users::table {}
impl ::diesel::query_source::TableNotEqual<schema::users::table> for comments::table {}
impl ::diesel::query_source::TableNotEqual<comments::table> for schema::posts::table {}
impl ::diesel::query_source::TableNotEqual<schema::posts::table> for comments::table {}