go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly

Home Page:https://gorm.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Null Fields When Using FullSaveAssociations and Updates

luizcarlosrodrigues opened this issue · comments

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 *string gorm:"column:office_id;not null;default:null" json:"officeId"
PersonTypeCode *int gorm:"column:person_type_code;not null;default:null" json:"personTypeCode"
Name *string gorm:"column:name;not null;default:null" json:"name"
DocumentTypeCode *int gorm:"column:document_type_code" json:"documentTypeCode,omitempty"
DocumentNumber *string gorm:"column:document_number" json:"documentNumber,omitempty"
DocumentIssueDate *time.Time gorm:"column:document_issue_date" json:"documentIssueDate,omitempty"
Birthday *time.Time gorm:"column:birthday" json:"birthday,omitempty"
RatingTypeCode *int gorm:"column:rating_type_code" json:"ratingTypeCode,omitempty"
CountryCode *string gorm:"column:country_code;not null;default:null" json:"countryCode"
Addresses []*PersonAddress gorm:"foreignKey:PersonId" json:"addresses,omitempty"
}

type PersonAddress struct {
AddressId *string gorm:"column:uuid;primaryKey;not null;default:null" json:"addressId"
OfficeId *string gorm:"column:office_id;not null;default:null" json:"officeId"
PersonId *string gorm:"column:person_id;not null;default:null" json:"clientId"
CityCode *int gorm:"column:city_code" json:"cityCode,omitempty"
CityName *string gorm:"column:city_name" json:"cityName,omitempty"
StreetName *string gorm:"column:street_name" json:"streetName,omitempty"
PostalCode *string gorm:"column:postal_code" json:"postalCode,omitempty"
AdditionalInfo *string gorm:"column:additional_info" json:"additionalInfo,omitempty"
MainAddress bool gorm:"column:main_address;not null;default:false" json:"mainAddress"
StatusCode *int gorm:"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.