Skip to content

Commit

Permalink
fix replace associations with custom pk
Browse files Browse the repository at this point in the history
  • Loading branch information
sgsv committed Jun 23, 2024
1 parent 8a0af58 commit 290a62e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
15 changes: 14 additions & 1 deletion callbacks/associations.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,20 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
} else if ref.PrimaryValue != "" {
db.AddError(ref.ForeignKey.Set(db.Statement.Context, f, ref.PrimaryValue))
}
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)

if ref.ForeignKey.Schema != nil {
for _, field := range ref.ForeignKey.Schema.Fields {
if field.PrimaryKey {
continue
}

if field.DBName == "" {
continue
}

assignmentColumns = append(assignmentColumns, field.DBName)
}
}
}

saveAssociations(db, rel, f, selectColumns, restricted, assignmentColumns)
Expand Down
32 changes: 28 additions & 4 deletions tests/associations_has_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,38 @@ func TestPolymorphicHasOneAssociationForSlice(t *testing.T) {
AssertAssociationCount(t, pets, "Toy", 0, "After Clear")
}

func TestHasOneAssociationReplaceWithNonValidValue(t *testing.T) {
user := User{Name: "jinzhu", Account: Account{Number: "1"}}
func TestReplaceHasOneAssociationWithCustomPK(t *testing.T) {
DB.Migrator().DropTable(&User{})
DB.AutoMigrate(&User{})
DB.AutoMigrate(&CreditCard{})

user := User{
Name: "jinzhu",
CreditCard: CreditCard{
Number: "123",
UserName: "jinzhu",
},
}

if err := DB.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}

if err := DB.Model(&user).Association("Languages").Replace(Account{Number: "2"}); err == nil {
t.Error("expected association error to be not nil")
wantNumber := "456"

if err := DB.Model(&user).Association("CreditCard").Replace(&CreditCard{
Number: wantNumber,
UserName: "jinzhu",
}); err != nil {
t.Fatalf("errors happened when create: %v", err)
}

var result User
if err := DB.Preload("CreditCard").First(&result, user.ID).Error; err != nil {
t.Fatalf("errors happened when getting credit card: %v", err)
}

if result.CreditCard.Number != wantNumber {
t.Fatal("wrong credit card number")
}
}
38 changes: 22 additions & 16 deletions utils/tests/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ import (
// NamedPet is a reference to a named `Pet` (has one)
type User struct {
gorm.Model
Name string
Age uint
Birthday *time.Time
Account Account
Pets []*Pet
NamedPet *Pet
Toys []Toy `gorm:"polymorphic:Owner"`
Tools []Tools `gorm:"polymorphicType:Type;polymorphicId:CustomID"`
CompanyID *int
Company Company
ManagerID *uint
Manager *User
Team []User `gorm:"foreignkey:ManagerID"`
Languages []Language `gorm:"many2many:UserSpeak;"`
Friends []*User `gorm:"many2many:user_friends;"`
Active bool
Name string
Age uint
Birthday *time.Time
Account Account
Pets []*Pet
NamedPet *Pet
Toys []Toy `gorm:"polymorphic:Owner"`
Tools []Tools `gorm:"polymorphicType:Type;polymorphicId:CustomID"`
CompanyID *int
Company Company
ManagerID *uint
Manager *User
Team []User `gorm:"foreignkey:ManagerID"`
Languages []Language `gorm:"many2many:UserSpeak;"`
Friends []*User `gorm:"many2many:user_friends;"`
Active bool
CreditCard CreditCard `gorm:"foreignKey:UserName;references:name"`
}

type Account struct {
Expand Down Expand Up @@ -102,3 +103,8 @@ type Child struct {
ParentID *uint
Parent *Parent
}

type CreditCard struct {
Number string
UserName string `gorm:"primaryKey;unique;size:255"`
}

0 comments on commit 290a62e

Please sign in to comment.