为了账号安全,请及时绑定邮箱和手机立即绑定

Postgres 返回 []uint8 而不是 []integer

Postgres 返回 []uint8 而不是 []integer

Go
沧海一幻觉 2022-06-01 10:07:58
我将整数数组保存到 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 界面使用相同的支持类型。


查看完整回答
反对 回复 2022-06-01
  • 1 回答
  • 0 关注
  • 148 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号