我正在将整数数组保存到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
,则会得到字节。
我正在将整数数组保存到PostgreSQL表中,并且在尝试检索它时,我总是得到[] uint8而不是[] int。我尝试使用[] integer,[] bigint,[] smallint。什么都行不通。数组...
使用pq.Array(&temp.Quantity)
您的存储方式也必须采用这种方式。