title: Nested Joins with Preload in Gorm
body: <p>For our use case, we have below model structs and SQL table definition :</p>
  <pre class="lang-golang prettyprint-override"><code>type Person struct { 
     ID         string 
     Posts   []Post `gorm:&quot;foreignkey:PersonID;association_foreignkey:ID&quot;`
  }
  
  type Post { 
    PersonID     string 
    ConceptID    string 
    DraftID      string 
    Created_at time.Time 
    Status         string 
  } 
  </code></pre>
  <p>Each Person can have multiple Posts. Each Post can have multiple drafts and all drafts of a Post have the same ConceptID. This way, we can fetch all the Posts created by a person. Person can keep editing the Post and this will create a post with new DraftID while ConceptID will remain same and only one of the Post will be active at a time.</p>
  <p>Ex: if a person creates a Post, an entry is created in Post Table with below information:</p>
  <ul>
  <li>PersonID - p1</li>
  <li>ConceptID - c1</li>
  <li>DraftID - d1</li>
  <li>Created_at - t1</li>
  </ul>
  <p>Now when person edits the same Post, a new version is created with DraftID as d2.</p>
  <ul>
  <li>PersonID - p1</li>
  <li>ConceptID - c1</li>
  <li>DraftID - d2</li>
  <li>Created_at - t2</li>
  </ul>
  <p>Now, when same person creates a new Post, a new entry is created with ConceptID as c2, DraftID as d3.</p>
  <ul>
  <li>PersonID - p1</li>
  <li>ConceptID - c2</li>
  <li>DraftID - d3</li>
  <li>Created-at - t3.</li>
  </ul>
  <p><strong>Post Table</strong></p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>PersonID</th>
  <th>ConceptID</th>
  <th>DraftID</th>
  <th>created_at</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>P1</td>
  <td>C1</td>
  <td>D1</td>
  <td>t1</td>
  </tr>
  <tr>
  <td>P1</td>
  <td>C1</td>
  <td>D2</td>
  <td>t2</td>
  </tr>
  <tr>
  <td>P2</td>
  <td>C2</td>
  <td>D3</td>
  <td>t3</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Our read query be like below :</p>
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM Person person 
  INNER JOIN 
      (SELECT post.* FROM Post post 
       LEFT JOIN post self ON post.concept_id  = self.concept_id 
         AND post.created_at &lt; self.created_at
       WHERE self.concept_id is NULL) posts 
  ON posts.person_id = person.person_id 
  WHERE person.person_id IN (&quot;person id&quot;) 
  </code></pre>
  <p>How can I write equivalent Preload with gorm as I need nested JOIN as well while Preload will fire an a new query for one to many relation with defined associations.</p>