These are chat archives for jinzhu/gorm

13th
May 2018
Murugesan
@bhairavitech
May 13 2018 01:38

How to handle one to many relationships with AutoIncrement

I have orders and order_items tables

**Order**
ID            uint             `gorm:"primary_key;AUTO_INCREMENT"`  
CustomerId    int
Amount        float64
OrderItems    []OrderItem     `gorm:"foreignkey:OrderId;association_foreignkey:Id"`

**OrderItem**
ID            uint             `gorm:"primary_key;AUTO_INCREMENT"`
Order         Order         `gorm:"foreignkey:OrderId"` 
OrderId       uint
Item
Qty
Rate
Amount

I can able to insert using db.Create(&order) only by giving Order.Id, OrderItem.Id and OrderItem.OrderID. Since, both Id column is AutoIncrement, I would like to know, how to insert without Id column values in Json.


{      "ID" : 30,
    "CustomerId": 1,
    "Amount": 34.65,
    "OrderItems": [{
        "ID":44,
        "OrderId":30,
        "ItemId": 1,
        "Qty": 99,
        "Rate": 199,
        "Amount": 19701
      },
      {
          "ID":45,
        "OrderId":30,
        "ItemId": 3,
        "Qty": 77,
        "Rate": 35,
        "Amount": 2695
      },
      {
          "ID":46,
        "OrderId":30,
        "ItemId": 11,
        "Qty": 12,
        "Rate": 125,
        "Amount": 1500
      }]
  }