fixbug:当为非自增主键时,也不作为修改列

This commit is contained in:
zxysilent 2023-03-11 11:07:52 +08:00
parent 886f293211
commit e229346b00
2 changed files with 12 additions and 9 deletions

View File

@ -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
}

View File

@ -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
}