This commit is contained in:
Jinzhu 2013-11-03 10:18:16 +08:00
parent a135087af4
commit e2927e1ec5
1 changed files with 11 additions and 9 deletions

View File

@ -27,7 +27,7 @@ Yet Another ORM library for Go, aims for developer friendly
```go ```go
db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable") db, err = Open("postgres", "user=gorm dbname=gorm sslmode=disable")
// Gorm is goroutines friendly, so you can create a global variable to keep the connection and use it everywhere // Gorm is goroutines friendly, so you can create a global variable to keep the connection and use it everywhere like this
var DB gorm.DB var DB gorm.DB
@ -45,14 +45,15 @@ db.SetPool(100)
## Conventions ## Conventions
```go ```go
type User struct { // TableName: `users`, gorm will pluralize struct name as table name type User struct { // TableName: `users`, gorm will pluralize struct name as table name
Id int64 // Id: Database Primary key Id int64 // Id: Database Primary key
Birthday time.Time // Time Birthday time.Time // Time
Age int64 Age int64
Name string Name string
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically CreatedAt time.Time // Time of record is created, will be insert automatically
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically UpdatedAt time.Time // Time of record is updated, will be updated automatically
DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more DeletedAt time.Time // Time of record is deleted, refer Soft Delete for more
Email []Email // Embedded structs Email []Email // Embedded structs
BillingAddress Address // Embedded struct BillingAddress Address // Embedded struct
BillingAddressId int64 // Embedded struct BillingAddress's foreign key BillingAddressId int64 // Embedded struct BillingAddress's foreign key
@ -348,6 +349,7 @@ db.Where(User{Name: "noexisting_user"}).Attrs("age", 20).FirstOrInit(&user)
db.Where(User{Name: "Jinzhu"}).Attrs(User{Age: 30}).FirstOrInit(&user) db.Where(User{Name: "Jinzhu"}).Attrs(User{Age: 30}).FirstOrInit(&user)
//// SELECT * FROM USERS WHERE name = jinzhu'; //// SELECT * FROM USERS WHERE name = jinzhu';
//// User{Id: 111, Name: "Jinzhu", Age: 20} //// User{Id: 111, Name: "Jinzhu", Age: 20}
```
### FirstOrInit With Assign ### FirstOrInit With Assign