Add Method RecordNotFound()

This commit is contained in:
Jinzhu 2013-11-24 11:29:37 +08:00
parent 7a789c82df
commit 0e2bef7006
2 changed files with 10 additions and 0 deletions

View File

@ -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 // If no record found, gorm will return RecordNotFound error, you could check it with
db.Where("name = ?", "hello world").First(&User{}).Error == gorm.RecordNotFound 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 ## Advanced Usage With Query Chain

View File

@ -242,6 +242,10 @@ func (s *DB) NewRecord(value interface{}) bool {
return s.clone().do(value).model.primaryKeyZero() return s.clone().do(value).model.primaryKeyZero()
} }
func (s *DB) RecordNotFound() bool {
return s.Error == RecordNotFound
}
// Migrations // Migrations
func (s *DB) CreateTable(value interface{}) *DB { func (s *DB) CreateTable(value interface{}) *DB {
return s.clone().do(value).createTable().db return s.clone().do(value).createTable().db