From e2f8d29d7f2405d340be0fab05ef4b7e4b6b521e Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 23 Dec 2013 21:02:55 +0800 Subject: [PATCH] Add RecordNotFound tests for method Related --- README.md | 7 ++++++- gorm_test.go | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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 {