# Join table twice

**URL:** <https://discourse.diesel.rs/t/join-table-twice/142>\
**Category:** Uncategorized\
**Created:** [April 8, 2020, 5:43pm UTC](https://discourse.diesel.rs/t/join-table-twice/142 "2020-04-08T17:43:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlepikhin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/johnlepikhin/32/71_2.png) [@johnlepikhin](https://discourse.diesel.rs/u/johnlepikhin)\
**Post date:** [April 8, 2020, 5:43pm UTC](https://discourse.diesel.rs/t/join-table-twice/142/1 "2020-04-08T17:43:04Z")

</div>

Hi,

I have a table1 which refers to table2 by two columns. For example:

```
create table posts (
   author bigint references author(id),
   editor bigint references author(id),
);

```

Is there a way to join the same table twice?

---

<div class="post-metadata">

**Author:** ![weiznich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/weiznich/32/64_2.png) [@weiznich](https://discourse.diesel.rs/u/weiznich)\
**Post date:** [April 8, 2020, 6:59pm UTC](https://discourse.diesel.rs/t/join-table-twice/142/2 "2020-04-08T18:59:54Z")

</div>

If you are looking for a simple way to do this the answer is: No that’s currently not possible with diesel, as this would require support for aliases to generate a valid query. Otherwise you would end with invalid SQL like `SELECT * FROM posts INNER JOIN author ON author.id = posts.author INNER JOIN author ON author.id = posts.editor` where you cannot tell to which joined table `author` now exactly refers.

That said, it is possible to make something like this work with the currently released diesel version, but it will require quite some work. There is [this](https://github.com/diesel-rs/diesel/pull/2254) WIP PR to diesel that would add native type safe support for table aliases to diesel itself. You can write similar code in a third party crate, with the restriction that it will not have that nice integration with some diesel things. (I’ve done this for one of my own projects with basically that code). That implies that you need to write even more things manually that would be otherwise generated by some of the diesel macros, especially all those impls [here](https://github.com/diesel-rs/diesel/pull/2254/files#diff-5a44fe26307f80c6fb185825668a73bcR1150-R1183). If you go down that route and hit any roadblock just ask for help here.
