ozzo-dbx icon indicating copy to clipboard operation
ozzo-dbx copied to clipboard

How to get result assigned to a struct for array in array?

Open lawzava opened this issue 8 years ago • 2 comments

I have the following struct:

type Order struct {
	OrderResourceID string      `json:"orderResourceID" db:"orderResourceID"`
	OrderStatusID   int         `json:"orderStatusID" db:"orderStatusID"`
	ClientUsername  string      `json:"clientUsername" db:"clientUsername"`
	ClientFullName  string      `json:"clientFullName" db:"clientFullName"`
	ClientPhotoUrl  string      `json:"clientPhotoUrl" db:"clientPhotoUrl"`
	OrderItems      []OrderItem `json:"orderItems" db:"orderItems"`
}

type OrderItem struct {
	ProductTitle     string      `json:"productTitle" db:"productTitle"`
	ProductPrice     int         `json:"productPrice" db:"productPrice"`
	ProductSize      string      `json:"productSize" db:"productSize"`
	ProductSizePrice int         `json:"productSizePrice" db:"productSizePrice"`
	ProductExtra     []ItemExtra `json:"productExtra" db:"productExtra"`
}

type ItemExtra struct {
	ProductExtraTitle string `json:"productExtraTitle" db:"productExtraTitle"`
	ProductExtraPrice int    `json:"productExtraPrice" db:"productExtraPrice"`
}

How should database call look like to get it assigned to these slices?

Database returns json_array on the point of slices. And I get this error: Internal server error: sql: Scan error on column index 5: unsupported Scan, storing driver.Value type []uint8 into type *[]models.OrderItem"}.

I assign to the variable with .One(&order) method after query selection.

What to change so it would populate all arrays and that array of an array?

lawzava avatar Sep 06 '17 12:09 lawzava

You should create a new slice type and implement the Scanner interface and/or Valuer interface.

qiangxue avatar Sep 06 '17 14:09 qiangxue

Could you provide a short example? I'm not quite sure how to do it. @qiangxue

lawzava avatar Sep 06 '17 15:09 lawzava