Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions oracle/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func Update(db *gorm.DB) {
// Always use PL/SQL for RETURNING, just like delete callback
buildUpdatePLSQL(db)
} else {
if updateClause, ok := stmt.Clauses["UPDATE"].Expression.(clause.Update); ok {
if updateClause.Table.Name != "" {
stmt.Table = updateClause.Table.Name
}
}
// Use GORM's standard build for non-RETURNING updates
stmt.Build("UPDATE", "SET", "WHERE")
// Convert values for Oracle
Expand Down
12 changes: 12 additions & 0 deletions tests/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,3 +1163,15 @@ func TestUpdateCustomDataType(t *testing.T) {
t.Errorf("failed to update custom data type field: %v", err)
}
}

func TestUpdateWithCustomTableName(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the issue! Could you please uncomment the related test in TestToSQL in tests/sql_builder_test.go as well? Thanks a lot!

sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Clauses(clause.Update{
Table: clause.Table{Name: "custom_update_table"},
}).Where("id = ?", 200).
Updates(&User{Name: "patched", Age: 99})
})

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)

}
Loading