Skip to content

Commit 5de56b6

Browse files
Merge pull request #91 from oracle-samples/fix_issue_81
Fix issue 81
2 parents 016821e + 214a4b7 commit 5de56b6

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
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/sql_builder_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -859,13 +859,12 @@ func TestToSQL(t *testing.T) {
859859
})
860860
assertEqualSQL(t, `UPDATE "custom_update_table" SET "updated_at"=?,"name"='patched',"age"=99 WHERE id = 200`, sql)
861861

862-
// https://github.com/oracle-samples/gorm-oracle/issues/81
863-
// sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
864-
// return tx.Clauses(clause.Update{Table: clause.Table{Name: "custom_update_table"}}).
865-
// Where("id = ?", 200).
866-
// Updates(&User{Name: "patched", Age: 99})
867-
// })
868-
// 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)
862+
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
863+
return tx.Clauses(clause.Update{Table: clause.Table{Name: "custom_update_table"}}).
864+
Where("id = ?", 200).
865+
Updates(&User{Name: "patched", Age: 99})
866+
})
867+
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)
869868

870869
// update
871870
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {

0 commit comments

Comments
 (0)