我将整数数组保存到 PostgreSQL 表中,当尝试检索它时,我总是得到 []uint8 而不是 []int。我尝试使用 []integer、[]bigint、[]smallint。没有任何效果。该数组最多表示四个项目,每个项目在 1-100 之间,没有浮点数。我正在使用 Go,并且我有一个 []int 对象,这是字段:Quantity []int `json:"quantity" db:"quantity"`我正在尝试修复它,但找不到让 PostgreSQL 返回 []int 的方法。所有其他表字段都工作得很好。该Quantity字段的类型integer[]这是插入查询:"INSERT INTO products (product_id, name, manufacturer, image_url, quantity, amount, notes, store_id, store_name, owner_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", newProduct.UID, newProduct.Name, newProduct.Manufacturer, newProduct.Image, pq.Array(newProduct.Quantity), newProduct.Amount, newProduct.Notes, newProduct.StoreID, newProduct.StoreName, newProduct.OwnerID);这就是我尝试获取数据的方式。err := rows.Scan(&temp.ID, &temp.UID, &temp.Name, &temp.Manufacturer, &temp.Image, &temp.Amount, &temp.Notes, &temp.StoreID, &temp.OwnerID, &temp.StoreName, &temp.Quantity)问题仅在于数量。如果我改变我temp object的[]uint8而不是[]int我得到字节。
1 回答
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
利用pq.Array(&temp.Quantity)
你存储的方式,你也必须以这种方式检索。
err := rows.Scan(&temp.ID, &temp.UID, &temp.Name, &temp.Manufacturer,
&temp.Image, &temp.Amount, &temp.Notes,
&temp.StoreID, &temp.OwnerID, &temp.StoreName, pq.Array(&temp.Quantity))
而且您还必须使用支持的类型pq.Array()或实现Scanner接口。对于整数,您可以使用[]sql.NullInt64或[]int64。但最好对 Scanner 和 Valuer 界面使用相同的支持类型。
- 1 回答
- 0 关注
- 148 浏览
添加回答
举报
0/150
提交
取消
