Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null Fields When Using FullSaveAssociations and Updates #7122

Open
luizcarlosrodrigues opened this issue Jul 21, 2024 · 0 comments
Open

Null Fields When Using FullSaveAssociations and Updates #7122

luizcarlosrodrigues opened this issue Jul 21, 2024 · 0 comments
Assignees
Labels
type:question general questions

Comments

@luizcarlosrodrigues
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants