title: Golang: Gorm Error on insert or update on table violates foreign key contraint
body: <p>Im trying to create a REST service using <a href="https://gorm.io/index.html" rel="nofollow noreferrer">Gorm</a> that on startup will drop the database tables from my Postgres DB and then create and populate them with test data. The entities I am using are as follows:</p>
  <pre class="lang-golang prettyprint-override"><code>type Group struct {
      ID       uuid.UUID `gorm:&quot;PrimaryKey&quot; json:&quot;id&quot;`
      Name     string     `json:&quot;name&quot;`
      Sessions []Session `gorm:&quot;foreignKey:ID&quot; json:&quot;sessions&quot;`
  }
  
  type Session struct {
      ID      uuid.UUID `gorm:&quot;PrimaryKey&quot;`
      GroupID uuid.UUID `gorm:&quot;foreignKey:ID&quot;`
      Name    string
      From    time.Time
      To      time.Time
  }
  
  type Player struct {
      ID       uuid.UUID `gorm:&quot;PrimaryKey&quot; json:&quot;id&quot;`
      Sessions []Session `gorm:&quot;many2many:players_sessions&quot; json:&quot;sessions&quot;`
      Groups   []Group   `gorm:&quot;many2many:players_groups&quot; json:&quot;groups&quot;`
      Username string    `gorm:&quot;type:varchar;NOT NULL&quot; json:&quot;username&quot;`
      Cookie   string    `json:&quot;cookie&quot;`
  }
  </code></pre>
  <p>On startup, when the tables have been dropped they are recreated and populated using this code:</p>
  <pre class="lang-golang prettyprint-override"><code>func PopulateDB(db *gorm.DB) {
      db.AutoMigrate(&amp;entities.Group{}, &amp;entities.Player{}, &amp;entities.Session{})
  
      players := []entities.Player{
          {ID: uuid.New(), Username: &quot;Player 1&quot;, Cookie: &quot;&quot;},
          {ID: uuid.New(), Username: &quot;Player 2&quot;, Cookie: &quot;&quot;},
          {ID: uuid.New(), Username: &quot;Player 3&quot;, Cookie: &quot;&quot;},
      }
      for index := range players {
          db.Create(&amp;players[index])
      }
  
      group := entities.Group{ID: uuid.New(), Name: &quot;My Group&quot;}
      db.Create(&amp;group)
  
      sessions := []entities.Session{
          {
              ID: uuid.New(), 
              GroupID: group.ID,
              Name: &quot;Session 1&quot;, 
              From: time.Now(), 
              To: time.Now().Add(12 * time.Hour),
          },
          {
              ID:      uuid.New(),
              GroupID: group.ID,
              Name:    &quot;Session 2&quot;,
              From:    time.Now().Add(24 * time.Hour),
              To:      time.Now().Add(36 * time.Hour),
          },
          
      }
      for index := range sessions {
          db.Model(&amp;group).Association(&quot;Sessions&quot;).Append(&amp;sessions[index])
          // db.Create(&amp;sessions[index])
      }
  
      // Make player &gt;-&lt; groups connections
      for index := range players {
          db.Model(&amp;players[index]).Association(&quot;Groups&quot;).Append(&amp;group)
      }
  
      // Make player &gt;-&lt; session connections
      for index := range players {
          db.Model(&amp;players[index]).Association(&quot;Sessions&quot;).Append(&amp;sessions[0])
          if index%2 == 0 {
              db.Model(&amp;players[index]).Association(&quot;Sessions&quot;).Append(&amp;sessions[1])
          }
      }
  }
  </code></pre>
  <p>The issue I am having is that when the test data is inserted it returns the following error: <code>ERROR: insert or update on table &quot;sessions&quot; violates foreign key constraint &quot;fk_groups_sessions&quot; (SQLSTATE 23503)</code>. This occurs on the line <code>db.Model(&amp;group).Association(&quot;Sessions&quot;).Append(&amp;sessions[index])</code>.</p>
  <p>I'm under the impression that this is being caused by trying to insert a Session without a valid Group foreign key, but I'm not knowledgeable enough with Gorm to understand what I'm doing wrong. Any help would be much appreciated!</p>