forked from mirror/gorm
Add Method NewRecord
This commit is contained in:
parent
66ac04ba05
commit
660ee1ed73
12
README.md
12
README.md
|
@ -137,6 +137,18 @@ user := User{Name: "jinzhu", Age: 18, Birthday: time.Now()}
|
|||
db.Save(&user)
|
||||
```
|
||||
|
||||
### NewRecord
|
||||
|
||||
Returns true if object hasn’t been saved yet (`Id` is blank)
|
||||
|
||||
```go
|
||||
user := User{Name: "jinzhu", Age: 18, Birthday: time.Now()}
|
||||
db.NewRecord(user) // => true
|
||||
|
||||
db.Save(&user)
|
||||
db.NewRecord(user) // => false
|
||||
```
|
||||
|
||||
### Create With SubStruct
|
||||
|
||||
Refer [Query With Related](#query-with-related) for how to find associations
|
||||
|
|
|
@ -161,11 +161,20 @@ func TestFirstAndLast(t *testing.T) {
|
|||
func TestCreateAndUpdate(t *testing.T) {
|
||||
name, name2, new_name := "update", "update2", "new_update"
|
||||
user := User{Name: name, Age: 1, PasswordHash: []byte{'f', 'a', 'k', '4'}}
|
||||
|
||||
if !db.NewRecord(user) {
|
||||
t.Error("User should be new record")
|
||||
}
|
||||
|
||||
db.Save(&user)
|
||||
if user.Id == 0 {
|
||||
t.Errorf("Should have ID after create")
|
||||
}
|
||||
|
||||
if db.NewRecord(user) {
|
||||
t.Error("User should not new record after save")
|
||||
}
|
||||
|
||||
var u User
|
||||
db.First(&u, user.Id)
|
||||
if !reflect.DeepEqual(u.PasswordHash, []byte{'f', 'a', 'k', '4'}) {
|
||||
|
|
Loading…
Reference in New Issue