Merge pull request #527 from yanfali/master

Update README.md with more realistic Transaction example
This commit is contained in:
Jinzhu 2015-06-15 20:44:21 +08:00
commit b110fed642
1 changed files with 21 additions and 0 deletions

View File

@ -868,6 +868,27 @@ tx.Rollback()
tx.Commit()
```
### More Complex Example
```
func CreateAnimals(db *gorm.DB) err {
tx := db.Begin()
// Note the use of tx as the database handle once you are within a transaction
if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil {
tx.Rollback()
return err
}
if err := tx.Create(&Animal{Name: "Lion"}).Error; err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
```
## Scopes
```go