Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Fix update with Alias #1455

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion session_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,19 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
return 0, errors.New("No content found to be updated")
}

var tableAlias = session.engine.Quote(tableName)
if session.statement.TableAlias != "" {
switch session.engine.dialect.DBType() {
case core.MSSQL:
tableAlias = fmt.Sprintf("%s %s", tableAlias, session.statement.TableAlias)
default:
tableAlias = fmt.Sprintf("%s AS %s", tableAlias, session.statement.TableAlias)
}
}

sqlStr = fmt.Sprintf("UPDATE %v%v SET %v %v",
top,
session.engine.Quote(tableName),
tableAlias,
strings.Join(colNames, ", "),
condSQL)

Expand Down
31 changes: 31 additions & 0 deletions session_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1421,3 +1421,34 @@ func TestUpdateExprs(t *testing.T) {
assert.EqualValues(t, 2, ue.NumIssues)
assert.EqualValues(t, "lunny xiao", ue.Name)
}

func TestUpdateAlias(t *testing.T) {
assert.NoError(t, prepareEngine())

type UpdateAlias struct {
Id int64
NumIssues int
Name string
}

assertSync(t, new(UpdateAlias))

_, err := testEngine.Insert(&UpdateAlias{
NumIssues: 1,
Name: "lunny",
})
assert.NoError(t, err)

_, err = testEngine.Alias("ua").Where("ua.id = ?", 1).Update(&UpdateAlias{
NumIssues: 2,
Name: "lunny xiao",
})
assert.NoError(t, err)

var ue UpdateAlias
has, err := testEngine.Get(&ue)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, 2, ue.NumIssues)
assert.EqualValues(t, "lunny xiao", ue.Name)
}