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
|
// 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
|
||||||
|
|
4
main.go
4
main.go
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue