duanba8173 2019-08-18 07:21
浏览 32

如何将嵌入式结构自动迁移为json数据类型

I m trying to create a table in postgresql with gorm automigrate. I have a struct in which I have field of type as another struct (embedded struct). The datatype of this filed is json. But while doing automigrate this field is not getting created as a column in the table

I have tried to change the type of embedded struct to pointer, it didn't help

type Home struct{
    HomeID   string      `gorm:"type:varchar(65);primary_key"`
    Address  string      `gorm:"type:varchar(200)"  json:"address"`
    Location Coordinates `gorm:"type:json"          json:"location"`
}

type Coordinates struct{
    Latitude  float64 `json:"latitude"`
    Longitude float64 `json:"longitude"`
}

I want to create location column with json as datatype. But this filed is not getting create in automigrate

I could get it done by changing the datatype of Location field from Coordinates to []byte, this will solve the problem of automigrate. But i have a json in the following way

{
   "address": "some_address",
   "location":{
      "latitude": 34.9393932,
      "longitude": 28.2348793
   } 
}

I need to unmarshal this json to the Home struct. json.Unmarshal is failing if I change the datatype of Location to []byte

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 怎么改成循环输入删除(语言-c语言)
    • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
    • ¥15 pyqt5tools安装失败
    • ¥15 mmdetection
    • ¥15 nginx代理报502的错误
    • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
    • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
    • ¥100 描述一下元器件的基本功能,pcba板的基本原理
    • ¥15 STM32无法向设备写入固件
    • ¥15 使用ESP8266连接阿里云出现问题