@kireke

Ошибка ERROR: column «order» is of type json[] but expression is of type record go gorm что делать?

Подскажите пожалуйста, у меня есть модель
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"`
}

когда сохраняю в базу данных получаю ошибку ERROR: column "order" is of type json[] but expression is of type record (SQLSTATE 42804)
поле Order должен быть массивом из json.
Подскажите пожалуйста, что делать? Гуглил но не нашел(
Использую gorm, база данных postgresql.
  • Вопрос задан
  • 383 просмотра
Решения вопроса 1
EvgenyMamonov
@EvgenyMamonov Куратор тега Go
Senior software developer, system architect
Скорее всего должно быть как то так
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"`
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы