I have the following structs.
type Relation struct {
gorm.Model
OwnerID uint `json:"ownerID"`
OwnerType string `json:"ownerType"`
Addresses []Address `gorm:"polymorphic:Owner;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"addresses"`
}
type Company struct {
gorm.Model
Name string `json:"name"`
Relation Relation `gorm:"polymorphic:Owner;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"relation"`
}
type Address struct {
gorm.Model
OwnerID uint `json:"ownerID"`
OwnerType string `json:"ownerType"`
Country string `json:"country"`
Zip string `json:"zip"`
Number uint `json:"number,string"`
Addition string `json:"addition"`
Street string `json:"street"`
State string `json:"state"`
City string `json:"city"`
}
And the following handler
func UpdateCompany(db *database.Database) fiber.Handler {
return func(c *fiber.Ctx) error {
id, err := IDFromParams(c)
if err != nil {
return c.JSON(responseKit.ParameterMissing())
}
update := new(model.Company)
if err = json.Unmarshal(c.Body(), update); err != nil {
return c.JSON(responseKit.ParsingError())
}
if result := db.Session(&gorm.Session{FullSaveAssociations: true}).Where("id = ?", id).Debug().Updates(update); result.Error != nil {
return c.JSON(responseKit.RecordUpdateError())
}
updated := new(model.Company)
result := db.Preload("Relation.Addresses").
Find(updated, id)
if result.Error != nil {
return c.JSON(responseKit.RecordReadError())
}
return c.JSON(responseKit.RecordUpdatedSuccess(updated))
}
}
I read
https://gorm.io/docs/associations.html
and
https://github.com/go-gorm/gorm/issues/3487#issuecomment-698303344
but it does not seem to work.
I use a rest client to call the endpoint with the following JSON
{
"name": "yep",
"relation": {
"adresses": [
{
"number": "5"
}
]
}
}
And I parse it into a struct. When I print that struct update := new(model.Company)
it has the correct data. But when I run the update I get the following output?
INSERT INTO "addresses" ("created_at","updated_at","deleted_at","owner_id","owner_type","country","zip","number","addition","street","state","city") VALUES ('2021-01-14 10:48:56.399','2021-01-14 10:48:56.399',NULL,11,'relations','','',5,'','','','') ON CONFLICT ("id") DO UPDATE SET "created_at"="excluded"."created_at","updated_at"="excluded"."updated_at","deleted_at"="excluded"."deleted_at","owner_id"="excluded"."owner_id","owner_type"="excluded"."owner_type","country"="excluded"."country","zip"="excluded"."zip","number"="excluded"."number","addition"="excluded"."addition","street"="excluded"."street","state"="excluded"."state","city"="excluded"."city" RETURNING "id"
INSERT INTO "relations" ("created_at","updated_at","deleted_at","owner_id","owner_type") VALUES ('2021-01-14 10:48:56.399','2021-01-14 10:48:56.399',NULL,0,'companies') ON CONFLICT ("id") DO UPDATE SET "created_at"="excluded"."created_at","updated_at"="excluded"."updated_at","deleted_at"="excluded"."deleted_at","owner_id"="excluded"."owner_id","owner_type"="excluded"."owner_type" RETURNING "id"
UPDATE "companies" SET "updated_at"='2021-01-14 10:48:56.399',"name"='yep' WHERE id = 3
So it does inserts for it's relations. But I want it to update the records.