Reputation: 152
I'm working on a Go project using GORM to handle database operations, and I've encountered an issue when trying to update associations between two models: BookingConfig and ExtraConfig. Specifically, when I use the Replace method to update the ExtraConfigs associated with a BookingConfig, it unexpectedly sets the BookingConfigID foreign key in ExtraConfig entries to NULL, rather than maintaining the correct association.
Models: Here's a simplified version of my models:
type BookingConfig struct {
gorm.Model
BoatID uint
ExtraConfigs []*ExtraConfig
}
type ExtraConfig struct {
ExtraID uint
BookingConfigID uint
Price float64
}
Update Function: I'm attempting to update ExtraConfig entries related to a BookingConfig using the Replace method like this:
func (r *gormBoatRepository) updateExistingBoat(boat *domain.Boat) error {
tx := r.db.Begin()
if tx.Error != nil {
return tx.Error
}
//More code...
for _, bookingConfig := range boat.BookingConfigDB {
if err := tx.Model(&bookingConfig).Association("ExtraConfigs").Replace(bookingConfig.ExtraConfigs); err != nil {
tx.Rollback()
return err
}
}
return tx.Commit().Error
}
Before calling Replace, I ensure that each ExtraConfig has a BookingConfigID correctly set, but after Replace executes, all ExtraConfig entries related to the BookingConfig have their BookingConfigID set to NULL in the database. Although in the code they continue to be maintained with their appropriate value.
What I've Tried:
UPDATE extra_configs SET booking_config_id=NULL WHERE extra_configs.booking_config_id = 316
Currently I'm working with this "Manual Replace", but I would like to work with a the original method
func (r *gormBoatRepository) updateExtraConfigs(tx *gorm.DB, boat *domain.Boat) error {
for _, bookingConfig := range boat.BookingConfigDB {
// Delete old ExtraConfigs.
if err := tx.Where("booking_config_id = ?", bookingConfig.ID).Delete(&domain.ExtraConfig{}).Error; err != nil {
return err
}
for _, extraConfig := range bookingConfig.ExtraConfigs {
extraConfig.BookingConfigID = bookingConfig.ID
if err := tx.Create(&extraConfig).Error; err != nil {
return err
}
}
}
return nil
}
Question: Has anyone encountered a similar issue with GORM's Replace method, or is there something I'm overlooking in how I'm using it? I need to replace the associated entries without losing the foreign key association in ExtraConfig.
Any insights or suggestions would be greatly appreciated. Thank you!
Update
After a whole year, I find exactly the same behaviour, and no solution:
if err := r.db.Unscoped().Model(season).Association("TimeSlots").Replace(season.TimeSlots); err != nil {
return err
}
return r.db.Omit("PricesDB", "TimeSlots").Session(&gorm.Session{FullSaveAssociations: true}).Save(season).Error
type Season struct {
gorm.Model
TimeSlots []*SeasonTimeSlot `json:"time_slots" gorm:"foreignKey:SeasonID"`
}
type SeasonTimeSlot struct {
SeasonID uint `json:"season_id"`
Type timeslot.TimeSlotType `json:"type"`
}
Upvotes: 3
Views: 527