mirror of https://github.com/go-gorm/gorm.git
Update README.md with Transaction example
- add a more detailed slightly more realistic example for a transaction.
This commit is contained in:
parent
cde05781a0
commit
5b282263d8
21
README.md
21
README.md
|
@ -868,6 +868,27 @@ tx.Rollback()
|
||||||
tx.Commit()
|
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
|
## Scopes
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
Loading…
Reference in New Issue