From d61af54b96cb2dc0806e2247df05582e79b2636b Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 13 Apr 2015 10:09:00 +0800 Subject: [PATCH] Add default model struct --- README.md | 17 ++++++++++++++++- model.go | 10 ++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 model.go diff --git a/README.md b/README.md index 0c4ea83a..4bcba989 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ type User struct { Num int `sql:"AUTO_INCREMENT"` CreatedAt time.Time UpdatedAt time.Time - DeletedAt time.Time + DeletedAt *time.Time Emails []Email // One-To-Many relationship (has many) 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 `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) +* 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 diff --git a/model.go b/model.go new file mode 100644 index 00000000..50fa52e6 --- /dev/null +++ b/model.go @@ -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 +}