From 4bf7c4d738ec527259be8722393973efc95c299d Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 14 Mar 2023 21:03:48 +0000 Subject: [PATCH] fix tidb Signed-off-by: Andrew Thornton --- internal/statements/upsert.go | 5 ++++- session_upsert.go | 11 +++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/statements/upsert.go b/internal/statements/upsert.go index c9e8a28f..70eb95c9 100644 --- a/internal/statements/upsert.go +++ b/internal/statements/upsert.go @@ -94,6 +94,9 @@ func (statement *Statement) GenUpsertSQL(doUpdate bool, columns []string, args [ for _, column := range updateColumns[1:] { write(", ", column, " = VALUES(", column, ")") } + if len(table.AutoIncrement) > 0 { + write(", ", quote(table.AutoIncrement), " = LAST_INSERT_ID(", quote(table.AutoIncrement), ")") + } } default: return "", nil, fmt.Errorf("unimplemented") // FIXME: UPSERT @@ -282,7 +285,7 @@ func (statement *Statement) GenUpsertMapSQL(doUpdate bool, columns []string, arg write(", ", column, " = VALUES(", column, ")") } if len(table.AutoIncrement) > 0 { - write(", ", quote(table.AutoIncrement), " = ", quote(table.AutoIncrement)) + write(", ", quote(table.AutoIncrement), " = LAST_INSERT_ID(", quote(table.AutoIncrement), ")") } } default: diff --git a/session_upsert.go b/session_upsert.go index 0946be9e..6a70a0f2 100644 --- a/session_upsert.go +++ b/session_upsert.go @@ -212,8 +212,6 @@ func (session *Session) upsertStruct(doUpdate bool, bean interface{}) (int64, er session.incrVersionFieldValue(verValue) } } - - id, iderr := res.LastInsertId() n, err := res.RowsAffected() if err != nil || n == 0 { return 0, err @@ -224,14 +222,15 @@ func (session *Session) upsertStruct(doUpdate bool, bean interface{}) (int64, er n = 1 } - if iderr != nil || id <= 0 { - return n, err - } - if table.AutoIncrement == "" { return n, nil } + id, err := res.LastInsertId() + if err != nil || id <= 0 { + return n, err + } + aiValue, err := table.AutoIncrColumn().ValueOf(bean) if err != nil { session.engine.logger.Errorf("%v", err)