1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-21 17:40:54 +00:00

Merge pull request #4158 from jianzhiyao/frt/fix_spk_upsert

fix:return error when calling ``InsertOrUpdate`` is successful with string primary key
This commit is contained in:
Ming Deng 2020-08-16 21:21:25 +08:00 committed by GitHub
commit 94f476fa39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 10 deletions

View File

@ -486,11 +486,13 @@ func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []s
return res.RowsAffected()
}
lastInsertId, err:=res.LastInsertId()
lastInsertId, err := res.LastInsertId()
if err != nil {
DebugLog.Println("[WARN] return LastInsertId error:", err)
DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)
return lastInsertId, ErrLastInsertIdUnavailable
}else{
return lastInsertId, nil
}
return lastInsertId, nil
}
return 0, err
}
@ -591,7 +593,14 @@ func (d *dbBase) InsertOrUpdate(q dbQuerier, mi *modelInfo, ind reflect.Value, a
if isMulti {
return res.RowsAffected()
}
return res.LastInsertId()
lastInsertId, err := res.LastInsertId()
if err != nil {
DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)
return lastInsertId, ErrLastInsertIdUnavailable
}else{
return lastInsertId, nil
}
}
return 0, err
}

View File

@ -164,7 +164,14 @@ func (d *dbBaseMysql) InsertOrUpdate(q dbQuerier, mi *modelInfo, ind reflect.Val
if isMulti {
return res.RowsAffected()
}
return res.LastInsertId()
lastInsertId, err := res.LastInsertId()
if err != nil {
DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)
return lastInsertId, ErrLastInsertIdUnavailable
}else{
return lastInsertId, nil
}
}
return 0, err
}

View File

@ -153,9 +153,11 @@ func (d *dbBaseOracle) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, nam
lastInsertId, err := res.LastInsertId()
if err != nil {
DebugLog.Println("[WARN] return LastInsertId error:", err)
DebugLog.Println(ErrLastInsertIdUnavailable, ':', err)
return lastInsertId, ErrLastInsertIdUnavailable
}else{
return lastInsertId, nil
}
return lastInsertId, nil
}
return 0, err
}

View File

@ -85,6 +85,8 @@ var (
ErrStmtClosed = errors.New("<QuerySeter> stmt already closed")
ErrArgs = errors.New("<Ormer> args error may be empty")
ErrNotImplement = errors.New("have not implement")
ErrLastInsertIdUnavailable = errors.New("<Ormer> last insert id is unavailable")
)
// Params stores the Params

View File

@ -2542,18 +2542,42 @@ func TestInsertOrUpdate(t *testing.T) {
func TestStrPkInsert(t *testing.T) {
RegisterModel(new(StrPk))
pk := `1`
value := `StrPkValues(*56`
strPk := &StrPk{
Id: "1",
Id: pk,
Value: value,
}
var err error
_, err = dORM.Insert(strPk)
throwFailNow(t, AssertIs(err, nil))
if err != ErrLastInsertIdUnavailable {
throwFailNow(t, AssertIs(err, nil))
}
var vForTesting StrPk
err = dORM.QueryTable(new(StrPk)).Filter(`id`, `1`).One(&vForTesting)
err = dORM.QueryTable(new(StrPk)).Filter(`id`, pk).One(&vForTesting)
throwFailNow(t, AssertIs(err, nil))
throwFailNow(t, AssertIs(vForTesting.Value, value))
value2 := `s8s5da7as`
strPkForUpsert := &StrPk{
Id: pk,
Value: value2,
}
_, err = dORM.InsertOrUpdate(strPkForUpsert, `id`)
if err != nil {
fmt.Println(err)
if err.Error() == "postgres version must 9.5 or higher" || err.Error() == "`sqlite3` nonsupport InsertOrUpdate in beego" {
} else if err == ErrLastInsertIdUnavailable {
} else {
throwFailNow(t, err)
}
} else {
var vForTesting2 StrPk
err = dORM.QueryTable(new(StrPk)).Filter(`id`, pk).One(&vForTesting2)
throwFailNow(t, AssertIs(err, nil))
throwFailNow(t, AssertIs(vForTesting2.Value, value2))
}
}