forked from mirror/gorm
Update README for Preload
This commit is contained in:
parent
24e0de116a
commit
0ea51c1e1f
23
README.md
23
README.md
|
@ -12,6 +12,7 @@ The fantastic ORM library for Golang, aims to be developer friendly.
|
||||||
* Callbacks (before/after create/save/update/delete/find)
|
* Callbacks (before/after create/save/update/delete/find)
|
||||||
* Soft Deletes
|
* Soft Deletes
|
||||||
* Auto Migrations
|
* Auto Migrations
|
||||||
|
* Preloading (eager loading)
|
||||||
* Transactions
|
* Transactions
|
||||||
* Customizable Logger
|
* Customizable Logger
|
||||||
* Iteration Support via [Rows](#row--rows)
|
* Iteration Support via [Rows](#row--rows)
|
||||||
|
@ -335,6 +336,28 @@ db.Where("name <> ?","jinzhu").Where("age >= ? and role <> ?",20,"admin").Find(&
|
||||||
db.Where("role = ?", "admin").Or("role = ?", "super_admin").Not("name = ?", "jinzhu").Find(&users)
|
db.Where("role = ?", "admin").Or("role = ?", "super_admin").Not("name = ?", "jinzhu").Find(&users)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Preloading (Eager loading)
|
||||||
|
|
||||||
|
```go
|
||||||
|
db.Preload("Orders").Find(&users)
|
||||||
|
//// SELECT * FROM users;
|
||||||
|
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4);
|
||||||
|
|
||||||
|
db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
|
||||||
|
//// SELECT * FROM users;
|
||||||
|
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4) AND state NOT IN ('cancelled');
|
||||||
|
|
||||||
|
db.Where("state = ?", "active").Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
|
||||||
|
//// SELECT * FROM users WHERE state = 'active';
|
||||||
|
//// SELECT * FROM orders WHERE user_id IN (1,2) AND state NOT IN ('cancelled');
|
||||||
|
|
||||||
|
db.Preload("Orders").Preload("Profile").Preload("Role").Find(&users)
|
||||||
|
//// SELECT * FROM users;
|
||||||
|
//// SELECT * FROM orders WHERE user_id IN (1,2,3,4); // has many
|
||||||
|
//// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one
|
||||||
|
//// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to
|
||||||
|
```
|
||||||
|
|
||||||
## Update
|
## Update
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
Loading…
Reference in New Issue