I have this struct in model package
type Item struct {
LineItemID uint `json:"lineItemId" gorm:"primaryKey"`
ItemCode string `json:"itemCode"`
Description string `json:"description"`
Quantity int `json:"quantity"`
OrderID int `json:"-"`
}
type Order struct {
OrderID uint `json:"orderId" gorm:"primaryKey"`
CustomerName string `json:"customerName"`
OrderedAt time.Time `json:"orderedAt"`
Items []Item `json:"items" gorm:"foreignKey:OrderID"`
}
Then i make handler for update the data:
func UpdateOrderById(c *gin.Context) {
id := c.Params.ByName("id")
var order models.Order
if err := config.DB.Preload("Items").First(&order, id).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
return
}
c.ShouldBindJSON(&order)
if err := config.DB.Save(&order).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record can't update!"})
return
}
c.JSON(http.StatusOK, order)
}
When i'm trying to use Postman for test my endpoint to update data, order table is successfully updated. But not in item table in database. Anyone can help me please?