Manuelarte
Manuelarte

Reputation: 1810

Gorm not updating array of child struct

I have the following structs

type Parent struct {
  ID       uuid     `gorm:"type:uuid" example:"070942aa-604f-42d2-bc6f-7295eae1929d"`
  Name     string
  Children []*Child `gorm:"constraint:OnUpdate:CASCADE"`

}
type Child struct {
   ID       uuid     `gorm:"type:uuid" example:"070942aa-604f-42d2-bc6f-7295eae1929d"`
   Age      int
}

and I want to update the parent entity, with updated information of the child/children. As an example, update the children ages through saving/updating the parent entity, I am using this :

gorm.Updates(&updatedParent)

With &updatedParent containing the updated information of the children. But I can update the parent fields (in this case Name), but I can't update the child entities (in this case age). Am I doing something wrong? I thought this gorm:"constraint:OnUpdate:CASCADE" should be sufficient

Upvotes: 2

Views: 981

Answers (1)

dnklgv
dnklgv

Reputation: 76

If you want to update associations data, you have to specify this explicitly like:

DB.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&parent)

See: https://gorm.io/docs/associations.html

Upvotes: 4

Related Questions