diff --git a/README.md b/README.md index 043c217a..f2b894cc 100644 --- a/README.md +++ b/README.md @@ -830,12 +830,17 @@ if err := db.Where("name = ?", "jinzhu").First(&user).Error; err != nil { // If no record found, gorm will return RecordNotFound error, you could check it with db.Where("name = ?", "hello world").First(&User{}).Error == gorm.RecordNotFound -// or use shortcut + +// Or use shortcut method if db.Where("name = ?", "hello world").First(&user).RecordNotFound() { panic("no record found") } else { user.Blalala() } + +if db.Model(&user).Related(&credit_card).RecordNotFound() { + panic("no credit card found") +} ``` ## Advanced Usage With Query Chain diff --git a/gorm_test.go b/gorm_test.go index d8ae5596..ac8db508 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -1282,6 +1282,10 @@ func TestRelated(t *testing.T) { if user3.Id != user.Id || user3.Name != user.Name { t.Errorf("Should get user from credit card correctly") } + + if !db.Model(&CreditCard{}).Related(&User{}).RecordNotFound() { + t.Errorf("RecordNotFound for Related") + } } type Order struct {