diff --git a/callback_query_preload.go b/callback_query_preload.go index 664d4fff..9163c6ba 100644 --- a/callback_query_preload.go +++ b/callback_query_preload.go @@ -77,6 +77,12 @@ func preloadCallback(scope *Scope) { } } + // copy error to original scope if needed + // this fixes https://github.com/jinzhu/gorm/issues/2122 + if currentScope.db != scope.db && currentScope.db.Error != nil { + scope.db.AddError(currentScope.db.Error) + } + // preload next level if idx < len(preloadFields)-1 { currentScope = currentScope.getColumnAsScope(preloadField) diff --git a/preload_test.go b/preload_test.go index 311ad0be..ffc2f54b 100644 --- a/preload_test.go +++ b/preload_test.go @@ -167,6 +167,43 @@ func TestNestedPreload1(t *testing.T) { } } +func TestNestedPreload1Error(t *testing.T) { + type ( + Level1 struct { + ID uint + Value string + Level2ID uint + } + Level2 struct { + ID uint + Level1 Level1 + Level3ID uint + } + Level3 struct { + ID uint + Name string + Level2 Level2 + } + ) + DB.DropTableIfExists(&Level3{}) + DB.DropTableIfExists(&Level2{}) + DB.DropTableIfExists(&Level1{}) + if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}).Error; err != nil { + t.Error(err) + } + + want := Level3{Level2: Level2{Level1: Level1{Value: "value"}}} + if err := DB.Create(&want).Error; err != nil { + t.Error(err) + } + + var got Level3 + if err := DB.Preload("Level2").Preload("Level2.Level1", "unknown_column=10").Find(&got).Error; err == nil { + t.Error("Expected error but got nil") + } + +} + func TestNestedPreload2(t *testing.T) { type ( Level1 struct {