Skip to content

Commit

Permalink
Fix FullSaveAssociations, close go-gorm#4874
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Nov 29, 2021
1 parent 9d5f315 commit e1b4c06
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
3 changes: 3 additions & 0 deletions callbacks/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
}

onConflict.DoUpdates = append(onConflict.DoUpdates, clause.AssignmentColumns(columns)...)
if len(onConflict.DoUpdates) == 0 {
onConflict.DoNothing = true
}

// use primary fields as default OnConflict columns
if len(onConflict.Columns) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions clause/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func (set Set) Build(builder Builder) {
builder.AddVar(builder, assignment.Value)
}
} else {
builder.WriteQuoted(PrimaryColumn)
builder.WriteQuoted(Column{Name: PrimaryKey})
builder.WriteByte('=')
builder.WriteQuoted(PrimaryColumn)
builder.WriteQuoted(Column{Name: PrimaryKey})
}
}

Expand Down
28 changes: 28 additions & 0 deletions tests/associations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,31 @@ func TestForeignKeyConstraintsBelongsTo(t *testing.T) {
t.Fatalf("Should not find deleted profile")
}
}

func TestFullSaveAssociations(t *testing.T) {
err := DB.
Session(&gorm.Session{FullSaveAssociations: true}).
Create(&Coupon{
ID: "full-save-association-coupon1",
AppliesToProduct: []*CouponProduct{
{
CouponId: "full-save-association-coupon1",
ProductId: "full-save-association-product1",
},
},
AmountOff: 10,
PercentOff: 0.0,
}).Error

if err != nil {
t.Errorf("Failed, got error: %v", err)
}

if DB.First(&Coupon{}, "id = ?", "full-save-association-coupon1").Error != nil {
t.Errorf("Failed to query saved coupon")
}

if DB.First(&CouponProduct{}, "coupon_id = ? AND product_id = ?", "full-save-association-coupon1", "full-save-association-product1").Error != nil {
t.Errorf("Failed to query saved association")
}
}
1 change: 1 addition & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/google/uuid v1.3.0
github.com/jackc/pgtype v1.9.1 // indirect
github.com/jackc/pgx/v4 v4.14.0 // indirect
github.com/jinzhu/now v1.1.3
github.com/lib/pq v1.10.4
Expand Down
2 changes: 1 addition & 1 deletion tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestMigrate(t *testing.T) {
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}, &Coupon{}, &CouponProduct{}}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })
DB.Migrator().DropTable("user_speaks", "user_friends", "ccc")
Expand Down
12 changes: 12 additions & 0 deletions utils/tests/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ type Language struct {
Code string `gorm:"primarykey"`
Name string
}

type Coupon struct {
ID string `gorm:"primarykey; size:255"`
AppliesToProduct []*CouponProduct `gorm:"foreignKey:CouponId;constraint:OnDelete:CASCADE"`
AmountOff uint32 `gorm:"amount_off"`
PercentOff float32 `gorm:"percent_off"`
}

type CouponProduct struct {
CouponId string `gorm:"primarykey; size:255"`
ProductId string `gorm:"primarykey; size:255"`
}

0 comments on commit e1b4c06

Please sign in to comment.