Add default model struct

This commit is contained in:
Jinzhu 2015-04-13 10:09:00 +08:00
parent 1eb1ed091f
commit d61af54b96
2 changed files with 26 additions and 1 deletions

View File

@ -39,7 +39,7 @@ type User struct {
Num int `sql:"AUTO_INCREMENT"` Num int `sql:"AUTO_INCREMENT"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
DeletedAt time.Time DeletedAt *time.Time
Emails []Email // One-To-Many relationship (has many) Emails []Email // One-To-Many relationship (has many)
BillingAddress Address // One-To-One relationship (has one) BillingAddress Address // One-To-One relationship (has one)
@ -84,6 +84,21 @@ type User struct{} // struct User's database table name is "users" by default, w
* Use `CreatedAt` to store record's created time if field exists * Use `CreatedAt` to store record's created time if field exists
* Use `UpdatedAt` to store record's updated time if field exists * Use `UpdatedAt` to store record's updated time if field exists
* Use `DeletedAt` to store record's deleted time if field exists [Soft Delete](#soft-delete) * Use `DeletedAt` to store record's deleted time if field exists [Soft Delete](#soft-delete)
* Gorm provide a default model struct, you could embed it in your struct
```go
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
type User struct {
gorm.Model
Name string
}
```
## Initialize Database ## Initialize Database

10
model.go Normal file
View File

@ -0,0 +1,10 @@
package gorm
import "time"
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}