diff --git a/internal/statements/update.go b/internal/statements/update.go index 40159e0c..4dcdf06c 100644 --- a/internal/statements/update.go +++ b/internal/statements/update.go @@ -19,11 +19,13 @@ import ( ) func (statement *Statement) ifAddColUpdate(col *schemas.Column, includeVersion, includeUpdated, includeNil, - includeAutoIncr, update bool) (bool, error) { + includeAutoIncr, isPrimaryKey, update bool) (bool, error) { columnMap := statement.ColumnMap omitColumnMap := statement.OmitColumnMap unscoped := statement.unscoped - + if !isPrimaryKey && col.IsPrimaryKey { + return false, nil + } if !includeVersion && col.IsVersion { return false, nil } @@ -64,7 +66,7 @@ func (statement *Statement) ifAddColUpdate(col *schemas.Column, includeVersion, // BuildUpdates auto generating update columnes and values according a struct func (statement *Statement) BuildUpdates(tableValue reflect.Value, includeVersion, includeUpdated, includeNil, - includeAutoIncr, update bool) ([]string, []interface{}, error) { + includeAutoIncr, isPrimaryKey, update bool) ([]string, []interface{}, error) { table := statement.RefTable allUseBool := statement.allUseBool useAllCols := statement.useAllCols @@ -76,7 +78,7 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value, for _, col := range table.Columns() { ok, err := statement.ifAddColUpdate(col, includeVersion, includeUpdated, includeNil, - includeAutoIncr, update) + includeAutoIncr, isPrimaryKey, update) if err != nil { return nil, nil, err } diff --git a/session_update.go b/session_update.go index 76f311d6..4d4efac2 100644 --- a/session_update.go +++ b/session_update.go @@ -145,9 +145,10 @@ func (session *Session) cacheUpdate(table *schemas.Table, tableName, sqlStr stri // Update records, bean's non-empty fields are updated contents, // condiBean' non-empty filds are conditions // CAUTION: -// 1.bool will defaultly be updated content nor conditions -// You should call UseBool if you have bool to use. -// 2.float32 & float64 may be not inexact as conditions +// +// 1.bool will defaultly be updated content nor conditions +// You should call UseBool if you have bool to use. +// 2.float32 & float64 may be not inexact as conditions func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int64, error) { if session.isAutoClose { defer session.Close() @@ -189,7 +190,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 if session.statement.ColumnStr() == "" { colNames, args, err = session.statement.BuildUpdates(v, false, false, - false, false, true) + false, false, false, true) } else { colNames, args, err = session.genUpdateColumns(bean) } @@ -480,7 +481,7 @@ func (session *Session) genUpdateColumns(bean interface{}) ([]string, []interfac args := make([]interface{}, 0, len(table.ColumnsSeq())) for _, col := range table.Columns() { - if !col.IsVersion && !col.IsCreated && !col.IsUpdated { + if !col.IsVersion && !col.IsCreated && !col.IsUpdated && !col.IsPrimaryKey { if session.statement.OmitColumnMap.Contain(col.Name) { continue }