title: Golang gorm join 2nd table data's now coming
body: <p>Am new in <code>Golang</code>, Please assume I have 2 models like below</p>
  <pre><code>type Jobs struct {
      JobID                  uint   `json: &quot;jobId&quot; gorm:&quot;primary_key;auto_increment&quot;`
      SourcePath             string `json: &quot;sourcePath&quot;`
      Priority               int64  `json: &quot;priority&quot;`
      InternalPriority       string `json: &quot;internalPriority&quot;`
      ExecutionEnvironmentID string `json: &quot;executionEnvironmentID&quot;`
  }
  
  type ExecutionEnvironment struct {
      ExecutionEnvironmentId string    `json: &quot;executionEnvironmentID&quot; gorm:&quot;primary_key;auto_increment&quot;`
      CloudProviderType      string    `json: &quot;cloudProviderType&quot;`
      InfrastructureType     string    `json: &quot;infrastructureType&quot;`
      CloudRegion            string    `json: &quot;cloudRegion&quot;`
      CreatedAt              time.Time `json: &quot;createdAt&quot;`
  }
  </code></pre>
  <p>This is my <code>Database connection</code> &amp; <code>get jobs</code> API code</p>
  <pre><code>package main
  
  import (
      &quot;encoding/json&quot;
      &quot;fmt&quot;
      &quot;net/http&quot;
      &quot;strconv&quot;
      &quot;time&quot;
  
      &quot;github.com/gorilla/mux&quot;
      &quot;github.com/jinzhu/gorm&quot;
      _ &quot;github.com/jinzhu/gorm/dialects/mysql&quot;
  )
  
  type Jobs struct {
      JobID                  uint   `json: &quot;jobId&quot; gorm:&quot;primary_key;auto_increment&quot;`
      SourcePath             string `json: &quot;sourcePath&quot;`
      Priority               int64  `json: &quot;priority&quot;`
      InternalPriority       string `json: &quot;internalPriority&quot;`
      ExecutionEnvironmentID string `json: &quot;executionEnvironmentID&quot;`
  }
  
  type ExecutionEnvironment struct {
      ExecutionEnvironmentId string    `json: &quot;executionEnvironmentID&quot; gorm:&quot;primary_key;auto_increment&quot;`
      CloudProviderType      string    `json: &quot;cloudProviderType&quot;`
      InfrastructureType     string    `json: &quot;infrastructureType&quot;`
      CloudRegion            string    `json: &quot;cloudRegion&quot;`
      CreatedAt              time.Time `json: &quot;createdAt&quot;`
  }
  
  var db *gorm.DB
  
  func initDB() {
      var err error
      dataSourceName := &quot;root:@tcp(localhost:3306)/?parseTime=True&quot;
      db, err = gorm.Open(&quot;mysql&quot;, dataSourceName)
  
      if err != nil {
          fmt.Println(err)
          panic(&quot;failed to connect database&quot;)
      }
      //db.Exec(&quot;CREATE DATABASE test&quot;)
      db.LogMode(true)
      db.Exec(&quot;USE test&quot;)
      db.AutoMigrate(&amp;Jobs{}, &amp;ExecutionEnvironment{})
  }
  
  func GetAllJobs(w http.ResponseWriter, r *http.Request) {
      w.Header().Set(&quot;Content-Type&quot;, &quot;application/json&quot;)
      fmt.Println(&quot;Executing Get All Jobs function&quot;)
      // var jobs []Jobs
      // db.Preload(&quot;jobs&quot;).Find(&amp;jobs)
      // json.NewEncoder(w).Encode(jobs)
      var jobs []Jobs
      if err := db.Table(&quot;jobs&quot;).Select(&quot;*&quot;).Joins(&quot;JOIN execution_environments on execution_environments.execution_environment_id = jobs.execution_environment_id&quot;).Find(&amp;jobs).Error; err != nil {
          fmt.Println(err)
      }
      json.NewEncoder(w).Encode(jobs)
  }
  
  
  func main() {
      // router
      router := mux.NewRouter()
      // Access URL
      router.HandleFunc(&quot;/GetAllJobs&quot;, GetAllJobs).Methods(&quot;GET&quot;)
      
      // Initialize db connection
      initDB()
  
      // config port
      fmt.Printf(&quot;Starting server at 8000 \n&quot;)
      http.ListenAndServe(&quot;:8000&quot;, router)
  }
  </code></pre>
  <p>In <code>GetAllJobs</code> I try to get <code>jobs</code> table data and <code>execution_environments</code> table data's. But my code not <code>fetch</code> <code>execution_environments</code> table data. Please check sample <code>json</code> below:</p>
  <pre><code>[
      {
          &quot;JobID&quot;: 4,
          &quot;SourcePath&quot;: &quot;test1&quot;,
          &quot;Priority&quot;: 2,
          &quot;InternalPriority&quot;: &quot;test&quot;,
          &quot;ExecutionEnvironmentID&quot;: &quot;103&quot;
      }
  ]
  </code></pre>
  <p><code>jobs table</code>
  <a href="https://i.stack.imgur.com/trCdx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/trCdx.png" alt="enter image description here" /></a></p>
  <p><code>execution_environments table</code>
  <a href="https://i.stack.imgur.com/EeOz0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EeOz0.png" alt="enter image description here" /></a></p>
  <p>Please help me to fetch the <code>execution_environments</code> table data's in my <code>json</code></p>