Skip to content

Commit 43c29f8

Browse files
committed
Fix TestCompositePrimaryKeysAssociations
Fix issue by excluding primary keys and default value fields from doUpdate.
1 parent 6ca987e commit 43c29f8

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

oracle/clause_builder.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,34 @@ func OnConflictClauseBuilder(c clause.Clause, builder clause.Builder) {
335335
return
336336
}
337337

338+
// exclude primary key, default value columns from merge update clause
339+
if len(onConflict.DoUpdates) > 0 {
340+
hasPrimaryKey := false
341+
342+
for _, assignment := range onConflict.DoUpdates {
343+
field := stmt.Schema.LookUpField(assignment.Column.Name)
344+
if field != nil && field.PrimaryKey {
345+
hasPrimaryKey = true
346+
break
347+
}
348+
}
349+
350+
if hasPrimaryKey {
351+
onConflict.DoUpdates = nil
352+
columns := make([]string, 0, len(values.Columns)-1)
353+
for _, col := range values.Columns {
354+
field := stmt.Schema.LookUpField(col.Name)
355+
356+
if field != nil && !field.PrimaryKey && (!field.HasDefaultValue || field.DefaultValueInterface != nil ||
357+
strings.EqualFold(field.DefaultValue, "NULL")) && field.AutoCreateTime == 0 {
358+
columns = append(columns, col.Name)
359+
}
360+
361+
}
362+
onConflict.DoUpdates = append(onConflict.DoUpdates, clause.AssignmentColumns(columns)...)
363+
}
364+
}
365+
338366
// Build MERGE statement
339367
buildMergeInClause(stmt, onConflict, values, conflictColumns)
340368
}

tests/multi_primary_keys_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,6 @@ func TestManyToManyWithCustomizedForeignKeys2(t *testing.T) {
454454
}
455455

456456
func TestCompositePrimaryKeysAssociations(t *testing.T) {
457-
t.Skip()
458-
459457
type Label struct {
460458
BookID *uint `gorm:"primarykey"`
461459
Name string `gorm:"primarykey"`

tests/passed-tests.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ TestMigrateExistingBoolColumnPG
192192
#TestManyToManyWithMultiPrimaryKeys
193193
#TestManyToManyWithCustomizedForeignKeys
194194
#TestManyToManyWithCustomizedForeignKeys2
195-
#TestCompositePrimaryKeysAssociations
195+
TestCompositePrimaryKeysAssociations
196196
TestNamedArg
197197
TestNamedArgMultipleSameParamRefs
198198
TestNamedArgNullValues

0 commit comments

Comments
 (0)