File tree 2 files changed +10
-1
lines changed
2 files changed +10
-1
lines changed Original file line number Diff line number Diff line change @@ -204,7 +204,7 @@ func Preload(db *gorm.DB) {
204
204
}
205
205
206
206
func AfterQuery (db * gorm.DB ) {
207
- if db .Error == nil && db .Statement .Schema != nil && ! db .Statement .SkipHooks && db .Statement .Schema .AfterFind {
207
+ if db .Error == nil && db .Statement .Schema != nil && ! db .Statement .SkipHooks && db .Statement .Schema .AfterFind && db . RowsAffected > 0 {
208
208
callMethod (db , func (value interface {}, tx * gorm.DB ) bool {
209
209
if i , ok := value .(AfterFindInterface ); ok {
210
210
db .AddError (i .AfterFind (tx ))
Original file line number Diff line number Diff line change @@ -133,6 +133,15 @@ func TestRunCallbacks(t *testing.T) {
133
133
if DB .Where ("Code = ?" , "unique_code" ).First (& p ).Error == nil {
134
134
t .Fatalf ("Can't find a deleted record" )
135
135
}
136
+
137
+ beforeCallTimes := p .AfterFindCallTimes
138
+ if DB .Where ("Code = ?" , "unique_code" ).Find (& p ).Error != nil {
139
+ t .Fatalf ("Find don't raise error when record not found" )
140
+ }
141
+
142
+ if p .AfterFindCallTimes != beforeCallTimes {
143
+ t .Fatalf ("AfterFind should not be called" )
144
+ }
136
145
}
137
146
138
147
func TestCallbacksWithErrors (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments