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.