fix: FirstOrCreate RowsAffected (#5250)
This commit is contained in:
		
							parent
							
								
									6aa6d37fc4
								
							
						
					
					
						commit
						a65912c588
					
				| @ -326,6 +326,9 @@ func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) { | |||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			return tx.Model(dest).Updates(assigns) | 			return tx.Model(dest).Updates(assigns) | ||||||
|  | 		} else { | ||||||
|  | 			// can not use Find RowsAffected
 | ||||||
|  | 			tx.RowsAffected = 0 | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return tx | 	return tx | ||||||
|  | |||||||
| @ -526,3 +526,17 @@ func TestCreateNilPointer(t *testing.T) { | |||||||
| 		t.Fatalf("it is not ErrInvalidValue") | 		t.Fatalf("it is not ErrInvalidValue") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestFirstOrCreateRowsAffected(t *testing.T) { | ||||||
|  | 	user := User{Name: "TestFirstOrCreateRowsAffected"} | ||||||
|  | 
 | ||||||
|  | 	res := DB.FirstOrCreate(&user, "name = ?", user.Name) | ||||||
|  | 	if res.Error != nil || res.RowsAffected != 1 { | ||||||
|  | 		t.Fatalf("first or create rows affect err:%v rows:%d", res.Error, res.RowsAffected) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	res = DB.FirstOrCreate(&user, "name = ?", user.Name) | ||||||
|  | 	if res.Error != nil || res.RowsAffected != 0 { | ||||||
|  | 		t.Fatalf("first or create rows affect err:%v rows:%d", res.Error, res.RowsAffected) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Cr
						Cr