forked from mirror/gorm
fix : update miss where's condition when primary key use "<-:create" tag (#4738)
* fix:update miss where condition * fix:rename test case
This commit is contained in:
parent
e3fc49a694
commit
b46e2afc4a
|
@ -235,7 +235,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
|
|||
case reflect.Struct:
|
||||
set = make([]clause.Assignment, 0, len(stmt.Schema.FieldsByDBName))
|
||||
for _, dbName := range stmt.Schema.DBNames {
|
||||
if field := updatingSchema.LookUpField(dbName); field != nil && field.Updatable {
|
||||
if field := updatingSchema.LookUpField(dbName); field != nil {
|
||||
if !field.PrimaryKey || !updatingValue.CanAddr() || stmt.Dest != stmt.Model {
|
||||
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && (!restricted || (!stmt.SkipHooks && field.AutoUpdateTime > 0))) {
|
||||
value, isZero := field.ValueOf(updatingValue)
|
||||
|
@ -252,7 +252,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
|
|||
isZero = false
|
||||
}
|
||||
|
||||
if ok || !isZero {
|
||||
if (ok || !isZero) && field.Updatable {
|
||||
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: value})
|
||||
assignValue(field, value)
|
||||
}
|
||||
|
|
|
@ -309,3 +309,22 @@ func TestFindOrCreate(t *testing.T) {
|
|||
t.Errorf("belongs to association should be saved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateWithMissWhere(t *testing.T) {
|
||||
type User struct {
|
||||
ID uint `gorm:"column:id;<-:create"`
|
||||
Name string `gorm:"column:name"`
|
||||
}
|
||||
user := User{ID: 1, Name: "king"}
|
||||
tx := DB.Session(&gorm.Session{DryRun: true}).Save(&user)
|
||||
|
||||
if err := tx.Error; err != nil {
|
||||
t.Fatalf("failed to update user,missing where condtion,err=%+v", err)
|
||||
|
||||
}
|
||||
|
||||
if !regexp.MustCompile("WHERE .id. = [^ ]+$").MatchString(tx.Statement.SQL.String()) {
|
||||
t.Fatalf("invalid updating SQL, got %v", tx.Statement.SQL.String())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue