Your Question
When using the statement below, some fields on associated table are been updated to null.
database.Db.WithContext(ctx).Session(&gorm.Session{FullSaveAssociations: true}).Clauses(clause.Returning{}).Updates(&client)
Here is my model:
type Persons struct { ClientId string
gorm:"column:uuid;primaryKey;not null;default:null" json:"clientId"OfficeId stringgorm:"column:office_id;not null;default:null" json:"officeId"PersonTypeCode intgorm:"column:person_type_code;not null;default:null" json:"personTypeCode"Name stringgorm:"column:name;not null;default:null" json:"name"DocumentTypeCode intgorm:"column:document_type_code" json:"documentTypeCode,omitempty"DocumentNumber stringgorm:"column:document_number" json:"documentNumber,omitempty"DocumentIssueDate time.Timegorm:"column:document_issue_date" json:"documentIssueDate,omitempty"Birthday time.Timegorm:"column:birthday" json:"birthday,omitempty"RatingTypeCode intgorm:"column:rating_type_code" json:"ratingTypeCode,omitempty"CountryCode stringgorm:"column:country_code;not null;default:null" json:"countryCode"Addresses []*PersonAddressgorm:"foreignKey:PersonId" json:"addresses,omitempty"}type PersonAddress struct { AddressId string
gorm:"column:uuid;primaryKey;not null;default:null" json:"addressId"OfficeId stringgorm:"column:office_id;not null;default:null" json:"officeId"PersonId stringgorm:"column:person_id;not null;default:null" json:"clientId"CityCode intgorm:"column:city_code" json:"cityCode,omitempty"CityName stringgorm:"column:city_name" json:"cityName,omitempty"StreetName stringgorm:"column:street_name" json:"streetName,omitempty"PostalCode stringgorm:"column:postal_code" json:"postalCode,omitempty"AdditionalInfo stringgorm:"column:additional_info" json:"additionalInfo,omitempty"MainAddress boolgorm:"column:main_address;not null;default:false" json:"mainAddress"StatusCode *intgorm:"column:status_code;not null;default:1" json:"statusCode"}
On this particular case I´m updating the statusCode on table PersonAddress but i don´t know what the transaction will update, it can update the person table and the table address, can create a new address and so on.
Now the scenarios that are working and not working are:
1) Updating just the parent table (works) 2) Updating the parent table and creating a new association record (works) 3) Updating the some parent table fields (works) but when updating some fields on the association it makes null all the other values not passed
The document you expected this should be explained
This doubt should be explanied on
https://gorm.io/docs/associations.html
Expected answer
I need a clear example on how I can use Updates with Association to make a full merge with what is in the database and what the user is updating/inserting.