K
K
kireke2021-08-17 19:08:14
go
kireke, 2021-08-17 19:08:14

ERROR: column "order" is of type json[] but expression is of type record go gorm what to do?

Please tell me I have a model

type Order struct {
  ID uint `gorm:"primaryKey;autoIncrement;unique"json:"id"`
  Owner string `json:"owner"`
  Status string `gorm:"default:ordered"json:"status"`
  Phone int `json:"phone"`
  Order []OrderStructure `gorm:"type:json[]"json:"order"`
}
type OrderStructure struct {
  ID uint `gorm:"primaryKey;autoIncrement;unique"json:"id"`
  Brand string `json:"brand"`
  ItemId int `json:"item_id"`
  Img string `json:"img"`
  ItemTotal int `json:"item_total"`
  Price string `json:"price"`
  Quantity int `json:"quantity"`
  Title string `json:"title"`
  Type string `json:"type"`
}

when I save to the database I get an error ERROR: column "order" is of type json[] but expression is of type record (SQLSTATE 42804)
the Order field must be an array of json.
Please tell me what to do? I googled but didn't find it(
I use gorm, postgresql database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-08-17
@kireke

Most likely it should be something like this

type Order struct {
  ID uint `gorm:"primaryKey;autoIncrement;unique"json:"id"`
  Owner string `json:"owner"`
  Status string `gorm:"default:ordered"json:"status"`
  Phone int `json:"phone"`
  Order []OrderProduct `gorm:"foreignKey:OrderId"json:"products"`
}

type OrderProduct struct {
  ID uint `gorm:"primaryKey;autoIncrement;unique"json:"id"`
   // добавляем это поле, чтобы была связь с таблицей Orders
  OrderID uint `json:"-"`
  Brand string `json:"brand"`
  ItemId int `json:"item_id"`
  Img string `json:"img"`
  ItemTotal int `json:"item_total"`
  Price string `json:"price"`
  Quantity int `json:"quantity"`
  Title string `json:"title"`
  Type string `json:"type"`
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question