Skip to content

Commit 5e16f5f

Browse files
committed
update clause with custom table name
1 parent 8748963 commit 5e16f5f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

oracle/update.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ func Update(db *gorm.DB) {
109109
// Always use PL/SQL for RETURNING, just like delete callback
110110
buildUpdatePLSQL(db)
111111
} else {
112+
if updateClause, ok := stmt.Clauses["UPDATE"].Expression.(clause.Update); ok {
113+
if updateClause.Table.Name != "" {
114+
stmt.Table = updateClause.Table.Name
115+
}
116+
}
112117
// Use GORM's standard build for non-RETURNING updates
113118
stmt.Build("UPDATE", "SET", "WHERE")
114119
// Convert values for Oracle

tests/migrate_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,3 +2178,15 @@ func TestMigrateOnUpdateConstraint(t *testing.T) {
21782178
panic(fmt.Errorf("company id is not equal: expects: %v, got: %v", 0, updatedPen3.OwnerID))
21792179
}
21802180
}
2181+
2182+
func TestUpdateWithCustomTableName(t *testing.T) {
2183+
sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
2184+
return tx.Clauses(clause.Update{
2185+
Table: clause.Table{Name: "custom_update_table"},
2186+
}).Where("id = ?", 200).
2187+
Updates(&User{Name: "patched", Age: 99})
2188+
})
2189+
2190+
assertEqualSQL(t, `UPDATE "custom_update_table" SET "updated_at"=?,"name"='patched',"age"=99 WHERE id = 200 AND "custom_update_table"."deleted_at" IS NULL`, sql)
2191+
2192+
}

0 commit comments

Comments
 (0)