我在 Postgres 中有一个 Jsonb 表
Create Table Business(
id serial not null primary key,
id_category integer not null,
name varchar(50) not null,
owner varchar(200) not null,
coordinates jsonb not null,
reason varchar(300) not null,
foreign key(id_category) references Category(id)
);
正如你所看到的,我将坐标存储为jsonb
前任:
Insert into Business(id_category, name, owner, coordinates, reason)
values
(1,'MyName','Owner', [{"latitude": 12.1268142, "longitude": -86.2754}]','Description')
我提取数据并分配它的方式是这样的。
type Business struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Owner string `json:"owner,omitempty"`
Category string `json:"category,omitempty"`
Departments []string `json:"departments,omitempty"`
Location []Coordinates `json:"location,omitempty"`
Reason string `json:"reason,omitempty"`
}
type Coordinates struct {
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
}
func (a Coordinates) Value() (driver.Value, error) {
return json.Marshal(a)
}
func (a *Coordinates) Scan(value []interface{}) error {
b, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
return json.Unmarshal(b, &a)
}
然而,我不断收到这样的消息。
sql:列索引 3 上的扫描错误,名称“坐标”:不支持的扫描,将 driver.Value 类型 []uint8 存储到类型 *models.Cooperatives 中
谁能告诉我如何解决这个问题?检索对象的 json 数组并将其分配给 Business 内的坐标字段。
拉丁的传说
三国纷争
相关分类