douyu9159 2019-09-13 02:35
浏览 23

嵌入的父级中包含的子级外键的注释

I am migrating a huge project (microservices) from java that handles json objects to golang with a relationnal database (postgresql). The json objects have known structures, that can easily ported to go structs.

I am adding json and gorm annotations to these structs so I can use old project code and new project code without rewriting front-end code.

I reached a case I don't know how to write gorm annotations to make it work.

The object is written in go like this (simplified) :

type MyObj struct {
    ID   int
    Name string
    Conf MyObjConf `gorm:"embedded;embedded_prefix:conf_;preload"`
}

type MyObjConf struct {
    ParamOne  string
    ParamTwo  int
    ParamList []ParamItem
}

type ParamItem struct {
    Key   string
    Value string
}
{
  "id": 1,
  "name": "hello",
  "conf": {
    "paramOne": "world",
    "paramTwo": 42,
    "paramList": [
      { "key": "captain", "value": "obvious" }
    ]
  }
}

In the database, it can be splitted into two tables, one containing MyObj and MyObjConf, and one containing lines of ParamItem.

I would like to declare them so when I db.AutoMigrate tables are created with a foreign key (delete cascade), when I select MyObj by id, everything is loaded (eager loading, may be set at query time if not possible at annotation level), when I save, paramlist is updated accordingly...

I am pretty sure that there are missing fields that will contain database relationnal data. I also think that if we add a new embedded annotation : embedded_id that for embedded structs would refer to embedder id, so we can define foreign keys like a "normal" table...

Any idea ? Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 易康econgnition精度验证
    • ¥15 线程问题判断多次进入
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致