mirror of https://github.com/go-gorm/gorm.git
Add Method RecordNotFound()
This commit is contained in:
parent
7a789c82df
commit
0e2bef7006
|
@ -827,6 +827,12 @@ 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
|
||||
if db.Where("name = ?", "hello world").First(&user).RecordNotFound() {
|
||||
panic("no record found")
|
||||
} else {
|
||||
user.Blalala()
|
||||
}
|
||||
```
|
||||
|
||||
## Advanced Usage With Query Chain
|
||||
|
|
4
main.go
4
main.go
|
@ -242,6 +242,10 @@ func (s *DB) NewRecord(value interface{}) bool {
|
|||
return s.clone().do(value).model.primaryKeyZero()
|
||||
}
|
||||
|
||||
func (s *DB) RecordNotFound() bool {
|
||||
return s.Error == RecordNotFound
|
||||
}
|
||||
|
||||
// Migrations
|
||||
func (s *DB) CreateTable(value interface{}) *DB {
|
||||
return s.clone().do(value).createTable().db
|
||||
|
|
Loading…
Reference in New Issue