Skip to content

Commit b289563

Browse files
committed
Remove unnecessary OpCreateValue mode
1 parent e015bd0 commit b289563

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

clause/association.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ package clause
44
type AssociationOpType int
55

66
const (
7-
OpUnlink AssociationOpType = iota // Unlink association
8-
OpDelete // Delete association records
9-
OpUpdate // Update association records
10-
OpCreate // Create association records with assignments
11-
OpCreateValues // Create association records with model object
7+
OpUnlink AssociationOpType = iota // Unlink association
8+
OpDelete // Delete association records
9+
OpUpdate // Update association records
10+
OpCreate // Create association records with assignments
1211
)
1312

1413
// Association represents an association operation
@@ -17,7 +16,6 @@ type Association struct {
1716
Type AssociationOpType // Operation type
1817
Conditions []Expression // Filter conditions
1918
Set []Assignment // Assignment operations (for Update and Create)
20-
Model interface{} // Model object (for Create object)
2119
Values []interface{} // Values for Create operation
2220
}
2321

generics.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,6 @@ func (s setCreateOrUpdateG[T]) executeAssociationOperation(ctx context.Context,
731731
switch op.Type {
732732
case clause.OpCreate:
733733
return s.handleAssociationCreate(ctx, base, op)
734-
case clause.OpCreateValues:
735-
return s.handleAssociationCreateValues(ctx, base, op)
736734
case clause.OpUnlink, clause.OpDelete, clause.OpUpdate:
737735
return s.handleAssociation(ctx, base, op)
738736
default:
@@ -741,16 +739,16 @@ func (s setCreateOrUpdateG[T]) executeAssociationOperation(ctx context.Context,
741739
}
742740

743741
func (s setCreateOrUpdateG[T]) handleAssociationCreate(ctx context.Context, base *DB, op clause.Association) error {
744-
return s.handleAssociationForOwners(base, ctx, func(owner T, assoc *Association) error {
745-
data := make(map[string]interface{}, len(op.Set))
746-
for _, a := range op.Set {
747-
data[a.Column.Name] = a.Value
748-
}
749-
return assoc.Append(data)
750-
}, op.Association)
751-
}
742+
if len(op.Set) > 0 {
743+
return s.handleAssociationForOwners(base, ctx, func(owner T, assoc *Association) error {
744+
data := make(map[string]interface{}, len(op.Set))
745+
for _, a := range op.Set {
746+
data[a.Column.Name] = a.Value
747+
}
748+
return assoc.Append(data)
749+
}, op.Association)
750+
}
752751

753-
func (s setCreateOrUpdateG[T]) handleAssociationCreateValues(ctx context.Context, base *DB, op clause.Association) error {
754752
return s.handleAssociationForOwners(base, ctx, func(owner T, assoc *Association) error {
755753
return assoc.Append(op.Values...)
756754
}, op.Association)

tests/association_generics_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,23 @@ func TestClauseAssociationSetUpdateWithOpUnlink(t *testing.T) {
277277
}
278278
}
279279

280-
// Test Set + Update with Association OpCreateValues operation using real database
280+
// Test Set + Update with Association OpCreate operation using real database
281281
func TestClauseAssociationSetUpdateWithOpCreateValues(t *testing.T) {
282282
ctx := context.Background()
283283

284284
// Create a user first using real database
285-
user := User{Name: "TestClauseAssociationSetUpdateWithOpCreateValues", Age: 25}
285+
user := User{Name: "TestClauseAssociationSetUpdateWithOpCreate", Age: 25}
286286
if err := DB.Create(&user).Error; err != nil {
287287
t.Fatalf("failed to create user: %v", err)
288288
}
289289

290290
// Create a pet object
291291
newPet := Pet{Name: "created-pet"}
292292

293-
// Test Set + Update with Association OpCreateValues
293+
// Test Set + Update with Association OpCreate
294294
assocOp := clause.Association{
295295
Association: "Pets",
296-
Type: clause.OpCreateValues,
296+
Type: clause.OpCreate,
297297
Values: []interface{}{&newPet},
298298
}
299299

@@ -343,7 +343,7 @@ func TestClauseAssociationSetCreateWithManyToMany(t *testing.T) {
343343
// Test Set + Update with many-to-many association
344344
assocOp := clause.Association{
345345
Association: "Languages",
346-
Type: clause.OpCreateValues,
346+
Type: clause.OpCreate,
347347
Values: []interface{}{langs[0], langs[1]},
348348
}
349349

@@ -402,7 +402,7 @@ func TestClauseAssociationSetCreateWithBelongsTo(t *testing.T) {
402402
}
403403
}
404404

405-
// BelongsTo: create and assign company via OpCreateValues
405+
// BelongsTo: create and assign company via OpCreate
406406
func TestClauseAssociationSetUpdateBelongsToCreateValues(t *testing.T) {
407407
ctx := context.Background()
408408

@@ -411,7 +411,7 @@ func TestClauseAssociationSetUpdateBelongsToCreateValues(t *testing.T) {
411411
t.Fatalf("failed to create user: %v", err)
412412
}
413413

414-
assocOp := clause.Association{Association: "Company", Type: clause.OpCreateValues, Values: []interface{}{Company{Name: "Belongs-To-Co"}}}
414+
assocOp := clause.Association{Association: "Company", Type: clause.OpCreate, Values: []interface{}{Company{Name: "Belongs-To-Co"}}}
415415
if rows, err := gorm.G[User](DB).Where("id = ?", user.ID).Set(assocOp).Update(ctx); err != nil {
416416
t.Fatalf("Set Update belongs-to create values failed: %v", err)
417417
} else if rows != 0 {
@@ -538,7 +538,7 @@ func TestClauseAssociationSetUpdatePolymorphicTools(t *testing.T) {
538538
t.Fatalf("create user: %v", err)
539539
}
540540

541-
createOp := clause.Association{Association: "Tools", Type: clause.OpCreateValues, Values: []interface{}{Tools{Name: "wrench"}}}
541+
createOp := clause.Association{Association: "Tools", Type: clause.OpCreate, Values: []interface{}{Tools{Name: "wrench"}}}
542542
if rows, err := gorm.G[User](DB).Where("id = ?", user.ID).Set(createOp).Update(ctx); err != nil {
543543
t.Fatalf("create tools: %v", err)
544544
} else if rows != 0 {
@@ -909,7 +909,7 @@ func TestClauseAssociationSetUpdateAndDeleteManyOwnersMany2Many(t *testing.T) {
909909
AssertAssociationCount(t, u2, "Languages", 1, "after delete")
910910
}
911911

912-
// Test Set + Update with has-one (NamedPet) using OpCreateValues
912+
// Test Set + Update with has-one (NamedPet) using OpCreate
913913
func TestClauseAssociationSetUpdateHasOneCreateValues(t *testing.T) {
914914
ctx := context.Background()
915915

@@ -920,7 +920,7 @@ func TestClauseAssociationSetUpdateHasOneCreateValues(t *testing.T) {
920920

921921
assocOp := clause.Association{
922922
Association: "NamedPet",
923-
Type: clause.OpCreateValues,
923+
Type: clause.OpCreate,
924924
Values: []interface{}{Pet{Name: "named-pet"}},
925925
}
926926

tests/generics_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,6 @@ func TestGenericsAssociation(t *testing.T) {
10131013
{clause.OpDelete, "OpDelete"},
10141014
{clause.OpUpdate, "OpUpdate"},
10151015
{clause.OpCreate, "OpCreate"},
1016-
{clause.OpCreateValues, "OpCreateValues"},
10171016
}
10181017

10191018
for _, op := range operations {

0 commit comments

Comments
 (0)