forked from mirror/gorm
Add RecordNotFound tests for method Related
This commit is contained in:
parent
b3d2f62f40
commit
e2f8d29d7f
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue