title: How to save one to one relations with Gorm?
body: <p>How do I save user's address relation with Gorm and Postgres?</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "fmt"
      "log"
  
      "github.com/jinzhu/gorm"
      _ "github.com/jinzhu/gorm/dialects/postgres"
  )
  
  var (
      pgUri = "postgres://postgres@127.0.0.1:5432/postgres?sslmode=disable"
  )
  
  type User struct {
      gorm.Model
      Email   string
      Address Address
  }
  
  type Address struct {
      gorm.Model
      Street  string
      City    string
      Country string
  }
  
  func main() {
      db, err := gorm.Open("postgres", pgUri)
      if err != nil {
          log.Fatalf("Failed to connect Postgres: %v\n", err)
      }
  
      // Checked two tables (user, address) created in database
      db.AutoMigrate(&amp;User{}, &amp;Address{})
      defer db.Close()
  
      u := User{
          Email: "some@one.com",
          Address: Address{
              Street:  "One street",
              City:    "Two city",
              Country: "Three country",
          },
      }
  
      fmt.Println(u)
  
      if err := db.Create(&amp;u).Error; err != nil {
          panic(err)
      }
  
      if err := db.Save(&amp;u).Error; err != nil {
          panic(err)
      }
  }
  </code></pre>
  
  <p>After I run it with <code>go run main.go</code>:</p>
  
  <pre class="lang-sh prettyprint-override"><code>{{0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC &lt;nil&gt;} some@one.com {{0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC &lt;nil&gt;} One street Two city Three country}}
  </code></pre>
  
  <p>It creates a new user but <strong>does not create any address</strong>.</p>