title: invalid field found: define a valid foreign key for relations or implement the Valuer/Scanner interface
body: <p>I need help. I'm creating a simple CRUD server using GORM &amp; Gin. But I've always encountered an error whenever I try to send a POST request to create a data. The endpoint is able to read the request data but the data is just never created as a table in GORM.</p>
  <p>This is the code.</p>
  <pre class="lang-golang prettyprint-override"><code>// structs.go
  type Orders struct {
      // gorm.Model
      OrderID      uint `gorm:&quot;primaryKey&quot;`
      CustomerName string
      OrderedAt    time.Time
      Items        Items
  }
  
  type Items struct {
      // gorm.Model
      ItemID      uint `gorm:&quot;primaryKey&quot;`
      ItemCode    int64
      Description string
      Quantity    int64
      OrderID     uint
  }
  </code></pre>
  <p>This is the endpoint controller</p>
  <pre class="lang-golang prettyprint-override"><code>type InDB struct {
      DB *gorm.DB
  }
  
  func (idb *InDB) CreateOrder(c *gin.Context) {
      var (
          order  structs.Orders
          item   structs.Items
          result gin.H
      )
  
      customerName := c.PostForm(&quot;customer_name&quot;)
      itemCode := c.PostForm(&quot;item_code&quot;)
      description := c.PostForm(&quot;description&quot;)
      quantity := c.PostForm(&quot;quantity&quot;)
  
      order.CustomerName = customerName
      order.OrderedAt = time.Now()
      item.ItemCode, _ = strconv.ParseInt(itemCode, 10, 64)
      item.Description = description
      item.Quantity, _ = strconv.ParseInt(quantity, 10, 64)
  
      err := idb.DB.Create(&amp;order)
      if err != nil {
          result = gin.H{
              &quot;result&quot;: &quot;Order data isn't created&quot;,
          }
          panic(err)
      }
  
      err = idb.DB.Create(&amp;item)
      if err != nil {
          result = gin.H{
              &quot;result&quot;: &quot;Item data isn't created&quot;,
          }
          panic(err)
      }
  
      result = gin.H{
          &quot;order&quot;: order,
          &quot;item&quot;:  item,
      }
  
      c.JSON(http.StatusOK, result)
  }
  
  func (idb *InDB) GetOrders(c *gin.Context) {
      var (
          allOrders []structs.Orders
          allItems  []structs.Items
          result    gin.H
      )
  
      idb.DB.Find(&amp;allOrders)
      idb.DB.Find(&amp;allItems)
  
      if len(allOrders) &lt; 1 || len(allItems) &lt; 1 {
          result = gin.H{
              &quot;result&quot;: nil,
          }
      } else {
          result = gin.H{
              &quot;orders&quot;: allOrders,
              &quot;items&quot;:  allItems,
          }
      }
  
      c.JSON(http.StatusOK, result)
  }
  </code></pre>
  <p>I used panic to check for the errors and the problem seems to be because of the foreign key. But I'm not sure how to fix it. This is the error:</p>
  <pre class="lang-sh prettyprint-override"><code>invalid field found for /structs.Orders's field Items: define a valid foreign key for relations or implement the Valuer/Scanner interface
  </code></pre>
  <p>Anyone can help me?</p>