fix: FirstOrCreate RowsAffected
This commit is contained in:
		
							parent
							
								
									74e07b049c
								
							
						
					
					
						commit
						2d835d560b
					
				@ -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