title: Go GORM nil relation being saved with zero struct values
body: <p>for some reason my GORM is trying to save a nil relation to the database as a zero struct and is then running into foreign key errors, because the foreign key ID is 0 (does not exist).</p>
  
  <p>I have the following model definitions:</p>
  
  <pre><code>type Member struct {
      gorm.Model
  
      EmployerID *uint    `json:"employer_id,omitempty"`
      Employer   Employer `json:"employer"`
  }
  
  type Employer struct {
    gorm.Model
  
    CompanyID uint     `json:"company_id"`
    Company   *Company `json:"company"`
  
    ExternalPayrollCompanyID *uint    `json:"external_payroll_company_id,omitempty"`
    ExternalPayrollCompany   *Company `json:"external_payroll_company,omitempty"`
  }
  
  
  type Company struct {
      gorm.Model
  
      AddressID        uint          `json:"address_id,omitempty"`
      Address          *Address      `json:"address,omitempty"`
  }
  
  type Address struct {
      gorm.Model
  
      StreetNr string `gorm:"type:varchar(600);not null" json:"street_nr,omitempty"`
      ZipCode  uint   `gorm:"type:int;not null" json:"zip_code,omitempty"`
      City     string `gorm:"type:varchar(600);not null" json:"city,omitempty"`
  }
  </code></pre>
  
  <p>As you can see, an <code>Employer</code> always has a <code>Company</code>, and possibly an <code>ExternalPayrollCompany</code>. If a <code>Company</code> exists, it always has an <code>Address</code>.</p>
  
  <p>I have a unit test, in which an <code>Employer</code> has a set <code>Company</code>, but a <code>nil</code> <code>ExternalPayrollCompany</code>. When I <code>Create</code> it in the database, GORM acts as expected (the <code>ExternalPayrollCompany</code>-ID is NULL, and no "empty" <code>Company</code> is created):</p>
  
  <pre><code>employer := Employer{
      Company: &amp;Company{
          Name: "123",
          Address: &amp;Address{
              StreetNr: "Asdf 13",
              ZipCode:  69999,
              City:     "Asdf",
          },
      },
  }
  db.Create(employer)
  </code></pre>
  
  <p>However, when the <code>Employer</code> is nested in a parent <code>Member</code> model:</p>
  
  <pre><code>member := Member{
      Employer: Employer{
          Company: &amp;Company{
              Name: "123",
              Address: &amp;Address{
                  StreetNr: "Asdf 13",
                  ZipCode:  69999,
                  City:     "Asdf",
              },
          },
      },
  }
  db.Create(member)
  </code></pre>
  
  <p>then the nil value for <code>ExternalPayrollCompany</code> is ignored, an empty <code>Company</code> with an <code>AddressID</code> of 0 is inserted, and the DB engine complains about a foreign key error, since no <code>Address</code> with ID 0 exists.</p>
  
  <p>It would also appear that GORM is initializing the <code>ExternalPayrollCompany</code> and then setting the timestamps, as the following is output in the debug mode:</p>
  
  <pre><code>INSERT INTO [companies] ([created_at],[updated_at],[name],[address_id]) OUTPUT Inserted.[id] VALUES ('2020-05-10 15:46:05','2020-05-10 15:46:05','',0)
  </code></pre>
  
  <p>I disabled the timestamp callback to no effect:</p>
  
  <pre><code>db.Callback().Create().Remove("gorm:update_time_stamp")
  </code></pre>
  
  <p>I would appreciate any input, since I am running out of ideas!</p>