1

I want to maintain a 1 to 1 relationship but whenever I try to save a record, the user table will get updated but there will be a new record created in the profile table rather than updating the associated profile record.

db.Model(&user).Save(&user)

Please let me know what i'm doing wrong here.

I have already referred to GORM Crud Documentation including the answer here https://stackoverflow.com/a/39333848/12066040

but seems like I'm missing something.

type User struct {
    UserID            uint `gorm:"primary_key"`
    Name              string

    // Profile
    FkProfileID uint    `gorm:"null;index; DEFAULT:null"`
    Profile     Profile `gorm:"foreignkey:FkProfileID"`
}

type Profile struct {
    ProfileID      uint `gorm:"primary_key"`
    ProfileSummary string
}

Expected Result:

Update both User and Profile tables when the profile record already exists for a user.

ciwanades
  • 11
  • 4
  • please provide a complete reproducible example. Let us know your db, or better use a docker container as a reference to reproduce that issue. –  Sep 14 '19 at 18:36

0 Answers0