forked from mirror/gorm
Document FirstOrInit, FirstOrCreate
This commit is contained in:
parent
1ecf9d76be
commit
e250637d12
18
README.md
18
README.md
|
@ -9,6 +9,7 @@ Yet Another ORM library for Go, aims for developer friendly
|
|||
* Before/After Create/Save/Update/Delete Callbacks
|
||||
* Order/Select/Limit/Offset Support
|
||||
* Update, Updates Like Rails's update_attribute, update_attributes
|
||||
* FirstOrInit, FirstOrCreate Like Rails's first_or_initialize, first_or_create
|
||||
* Dynamically set table name when search, update, delete...
|
||||
* Automatically CreatedAt, UpdatedAt
|
||||
* Soft Delete
|
||||
|
@ -84,6 +85,22 @@ db.Find(&users, &User{Age: 20})
|
|||
db.Find(&users, map[string]interface{}{"age": 20})
|
||||
//// users -> select * from users where age = 20;
|
||||
|
||||
// FirstOrInit
|
||||
db.FirstOrInit(&user, User{Name: "noexisting_user"})
|
||||
//// user -> User{Name: "noexisting_user"}
|
||||
db.Where(User{Name: "Jinzhu"}).FirstOrInit(&user)
|
||||
//// user -> User{Id: 111, Name: "Jinzhu"}
|
||||
db.FirstOrInit(&user, map[string]interface{}{"name": "jinzhu", "age": 20})
|
||||
//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}
|
||||
|
||||
// FirstOrCreate
|
||||
db.FirstOrCreate(&user, User{Name: "noexisting_user"})
|
||||
//// user -> User{Id: 112, Name: "noexisting_user"}
|
||||
db.Where(User{Name: "Jinzhu"}).FirstOrCreate(&user)
|
||||
//// user -> User{Id: 111, Name: "Jinzhu"}
|
||||
db.FirstOrCreate(&user, map[string]interface{}{"name": "jinzhu", "age": 20})
|
||||
//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}
|
||||
|
||||
// Select
|
||||
db.Select("name").Find(&users)
|
||||
//// users -> select name from users;
|
||||
|
@ -251,7 +268,6 @@ db.Where("mail_type = ?", "TEXT").Find(&users1).Table("deleted_users").First(&us
|
|||
```
|
||||
|
||||
## TODO
|
||||
* FindOrInitialize / FindOrCreate
|
||||
* SubStruct
|
||||
* Index, Unique, Valiations
|
||||
* Auto Migration
|
||||
|
|
22
gorm_test.go
22
gorm_test.go
|
@ -800,27 +800,37 @@ func TestSoftDelete(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestFindOrInitialize(t *testing.T) {
|
||||
var user1, user2 User
|
||||
var user1, user2, user3 User
|
||||
db.Where(&User{Name: "find or init", Age: 33}).FirstOrInit(&user1)
|
||||
if user1.Name != "find or init" || user1.Id != 0 || user1.Age != 33 {
|
||||
t.Errorf("user should be initialized with search value")
|
||||
}
|
||||
|
||||
db.FirstOrInit(&user2, map[string]interface{}{"name": "find or init 2"})
|
||||
if user2.Name != "find or init 2" || user2.Id != 0 {
|
||||
db.Where(User{Name: "find or init", Age: 33}).FirstOrInit(&user2)
|
||||
if user2.Name != "find or init" || user2.Id != 0 || user2.Age != 33 {
|
||||
t.Errorf("user should be initialized with search value")
|
||||
}
|
||||
|
||||
db.FirstOrInit(&user3, map[string]interface{}{"name": "find or init 2"})
|
||||
if user3.Name != "find or init 2" || user3.Id != 0 {
|
||||
t.Errorf("user should be initialized with inline search value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindOrCreate(t *testing.T) {
|
||||
var user1, user2 User
|
||||
var user1, user2, user3 User
|
||||
db.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user1)
|
||||
if user1.Name != "find or create" || user1.Id == 0 || user1.Age != 33 {
|
||||
t.Errorf("user should be created with search value")
|
||||
}
|
||||
|
||||
db.FirstOrCreate(&user2, map[string]interface{}{"name": "find or create 2"})
|
||||
if user2.Name != "find or create 2" || user2.Id == 0 {
|
||||
db.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user2)
|
||||
if user2.Name != "find or create" || user2.Id == 0 || user2.Age != 33 {
|
||||
t.Errorf("user should be created with search value")
|
||||
}
|
||||
|
||||
db.FirstOrCreate(&user3, map[string]interface{}{"name": "find or create 2"})
|
||||
if user3.Name != "find or create 2" || user3.Id == 0 {
|
||||
t.Errorf("user should be created with inline search value")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue