mirror of https://github.com/go-gorm/gorm.git
Merge pull request #222 from jaytaylor/readme-cleanup
README cleanup: Consistent comment prefixing `////` for raw SQL example output.
This commit is contained in:
commit
938c09e702
144
README.md
144
README.md
|
@ -52,40 +52,40 @@ go get -u github.com/jinzhu/gorm
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type User struct {
|
type User struct {
|
||||||
Id int64
|
Id int64
|
||||||
Birthday time.Time
|
Birthday time.Time
|
||||||
Age int64
|
Age int64
|
||||||
Name string `sql:"size:255"`
|
Name string `sql:"size:255"`
|
||||||
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)
|
||||||
BillingAddressId sql.NullInt64 // Foreign key of BillingAddress
|
BillingAddressId sql.NullInt64 // Foreign key of BillingAddress
|
||||||
ShippingAddress Address // One-To-One relationship (has one)
|
ShippingAddress Address // One-To-One relationship (has one)
|
||||||
ShippingAddressId int64 // Foreign key of ShippingAddress
|
ShippingAddressId int64 // Foreign key of ShippingAddress
|
||||||
IgnoreMe int64 `sql:"-"` // Ignore this field
|
IgnoreMe int64 `sql:"-"` // Ignore this field
|
||||||
Languages []Language `gorm:"many2many:user_languages;"` // Many-To-Many relationship, 'user_languages' is join table
|
Languages []Language `gorm:"many2many:user_languages;"` // Many-To-Many relationship, 'user_languages' is join table
|
||||||
}
|
}
|
||||||
|
|
||||||
type Email struct {
|
type Email struct {
|
||||||
Id int64
|
Id int64
|
||||||
UserId int64 // Foreign key for User (belongs to)
|
UserId int64 // Foreign key for User (belongs to)
|
||||||
Email string `sql:"type:varchar(100);"` // Set field's type
|
Email string `sql:"type:varchar(100);"` // Set field's type
|
||||||
Subscribed bool
|
Subscribed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Address struct {
|
type Address struct {
|
||||||
Id int64
|
Id int64
|
||||||
Address1 string `sql:"not null;unique"` // Set field as not nullable and unique
|
Address1 string `sql:"not null;unique"` // Set field as not nullable and unique
|
||||||
Address2 string `sql:"type:varchar(100);unique"`
|
Address2 string `sql:"type:varchar(100);unique"`
|
||||||
Post sql.NullString `sql:not null`
|
Post sql.NullString `sql:not null`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Language struct {
|
type Language struct {
|
||||||
Id int64
|
Id int64
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -94,10 +94,10 @@ type Language struct {
|
||||||
```go
|
```go
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||||
|
@ -172,11 +172,11 @@ db.Save(&user)
|
||||||
|
|
||||||
// Associations will be saved automatically when insert the record
|
// Associations will be saved automatically when insert the record
|
||||||
user := User{
|
user := User{
|
||||||
Name: "jinzhu",
|
Name: "jinzhu",
|
||||||
BillingAddress: Address{Address1: "Billing Address - Address 1"},
|
BillingAddress: Address{Address1: "Billing Address - Address 1"},
|
||||||
ShippingAddress: Address{Address1: "Shipping Address - Address 1"},
|
ShippingAddress: Address{Address1: "Shipping Address - Address 1"},
|
||||||
Emails: []Email{{Email: "jinzhu@example.com"}, {Email: "jinzhu-2@example@example.com"}},
|
Emails: []Email{{Email: "jinzhu@example.com"}, {Email: "jinzhu-2@example@example.com"}},
|
||||||
Languages: []Language{{Name: "ZH"}, {Name: "EN"}},
|
Languages: []Language{{Name: "ZH"}, {Name: "EN"}},
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Create(&user)
|
db.Create(&user)
|
||||||
|
@ -384,14 +384,14 @@ db.Model(User{}).Updates(User{Name: "hello", Age: 18}).RowsAffected
|
||||||
```go
|
```go
|
||||||
// Delete an existing record
|
// Delete an existing record
|
||||||
db.Delete(&email)
|
db.Delete(&email)
|
||||||
// DELETE from emails where id=10;
|
//// DELETE from emails where id=10;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Batch Delete
|
### Batch Delete
|
||||||
|
|
||||||
```go
|
```go
|
||||||
db.Where("email LIKE ?", "%jinzhu%").Delete(Email{})
|
db.Where("email LIKE ?", "%jinzhu%").Delete(Email{})
|
||||||
// DELETE from emails where email LIKE "%jinhu%";
|
//// DELETE from emails where email LIKE "%jinhu%";
|
||||||
```
|
```
|
||||||
|
|
||||||
### Soft Delete
|
### Soft Delete
|
||||||
|
@ -417,7 +417,7 @@ db.Unscoped().Where("age = 20").Find(&users)
|
||||||
|
|
||||||
// Delete record permanently with Unscoped
|
// Delete record permanently with Unscoped
|
||||||
db.Unscoped().Delete(&order)
|
db.Unscoped().Delete(&order)
|
||||||
// DELETE FROM orders WHERE id=10;
|
//// DELETE FROM orders WHERE id=10;
|
||||||
```
|
```
|
||||||
|
|
||||||
## Associations
|
## Associations
|
||||||
|
@ -700,18 +700,18 @@ row.Scan(&name, &age)
|
||||||
rows, err := db.Model(User{}).Where("name = ?", "jinzhu").Select("name, age, email").Rows() // (*sql.Rows, error)
|
rows, err := db.Model(User{}).Where("name = ?", "jinzhu").Select("name, age, email").Rows() // (*sql.Rows, error)
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
...
|
...
|
||||||
rows.Scan(&name, &age, &email)
|
rows.Scan(&name, &age, &email)
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
// Raw SQL
|
// Raw SQL
|
||||||
rows, err := db.Raw("select name, age, email from users where name = ?", "jinzhu").Rows() // (*sql.Rows, error)
|
rows, err := db.Raw("select name, age, email from users where name = ?", "jinzhu").Rows() // (*sql.Rows, error)
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
...
|
...
|
||||||
rows.Scan(&name, &age, &email)
|
rows.Scan(&name, &age, &email)
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -737,12 +737,12 @@ db.Raw("SELECT name, age FROM users WHERE name = ?", 3).Scan(&result)
|
||||||
```go
|
```go
|
||||||
rows, err := db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Rows()
|
rows, err := db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Rows()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Having("sum(amount) > ?", 100).Rows()
|
rows, err := db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Having("sum(amount) > ?", 100).Rows()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
type Result struct {
|
type Result struct {
|
||||||
|
@ -757,7 +757,7 @@ db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Grou
|
||||||
```go
|
```go
|
||||||
rows, err := db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Rows()
|
rows, err := db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Rows()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Scan(&results)
|
db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Scan(&results)
|
||||||
|
@ -782,21 +782,21 @@ tx.Commit()
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func AmountGreaterThan1000(db *gorm.DB) *gorm.DB {
|
func AmountGreaterThan1000(db *gorm.DB) *gorm.DB {
|
||||||
return db.Where("amount > ?", 1000)
|
return db.Where("amount > ?", 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PaidWithCreditCard(db *gorm.DB) *gorm.DB {
|
func PaidWithCreditCard(db *gorm.DB) *gorm.DB {
|
||||||
return db.Where("pay_mode_sign = ?", "C")
|
return db.Where("pay_mode_sign = ?", "C")
|
||||||
}
|
}
|
||||||
|
|
||||||
func PaidWithCod(db *gorm.DB) *gorm.DB {
|
func PaidWithCod(db *gorm.DB) *gorm.DB {
|
||||||
return db.Where("pay_mode_sign = ?", "C")
|
return db.Where("pay_mode_sign = ?", "C")
|
||||||
}
|
}
|
||||||
|
|
||||||
func OrderStatus(status []string) func (db *gorm.DB) *gorm.DB {
|
func OrderStatus(status []string) func (db *gorm.DB) *gorm.DB {
|
||||||
return func (db *gorm.DB) *gorm.DB {
|
return func (db *gorm.DB) *gorm.DB {
|
||||||
return db.Scopes(AmountGreaterThan1000).Where("status in (?)", status)
|
return db.Scopes(AmountGreaterThan1000).Where("status in (?)", status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Scopes(AmountGreaterThan1000, PaidWithCreditCard).Find(&orders)
|
db.Scopes(AmountGreaterThan1000, PaidWithCreditCard).Find(&orders)
|
||||||
|
@ -859,18 +859,18 @@ AfterFind
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (u *User) BeforeUpdate() (err error) {
|
func (u *User) BeforeUpdate() (err error) {
|
||||||
if u.readonly() {
|
if u.readonly() {
|
||||||
err = errors.New("read only user")
|
err = errors.New("read only user")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rollback the insertion if user's id greater than 1000
|
// Rollback the insertion if user's id greater than 1000
|
||||||
func (u *User) AfterCreate() (err error) {
|
func (u *User) AfterCreate() (err error) {
|
||||||
if (u.Id > 1000) {
|
if (u.Id > 1000) {
|
||||||
err = errors.New("user id is already greater than 1000")
|
err = errors.New("user id is already greater than 1000")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -881,8 +881,8 @@ Fortunately, gorm support pass transaction to callbacks as you needed, you could
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (u *User) AfterCreate(tx *gorm.DB) (err error) {
|
func (u *User) AfterCreate(tx *gorm.DB) (err error) {
|
||||||
tx.Model(u).Update("role", "admin")
|
tx.Model(u).Update("role", "admin")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -907,15 +907,15 @@ type Cart struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Cart) TableName() string {
|
func (c Cart) TableName() string {
|
||||||
return "shopping_cart"
|
return "shopping_cart"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u User) TableName() string {
|
func (u User) TableName() string {
|
||||||
if u.Role == "admin" {
|
if u.Role == "admin" {
|
||||||
return "admin_users"
|
return "admin_users"
|
||||||
} else {
|
} else {
|
||||||
return "users"
|
return "users"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -928,7 +928,7 @@ query := db.First(&user).Limit(10).Find(&users)
|
||||||
|
|
||||||
// So you could do error handing in your application like this:
|
// So you could do error handing in your application like this:
|
||||||
if err := db.Where("name = ?", "jinzhu").First(&user).Error; err != nil {
|
if err := db.Where("name = ?", "jinzhu").First(&user).Error; err != nil {
|
||||||
// error handling...
|
// error handling...
|
||||||
}
|
}
|
||||||
|
|
||||||
// RecordNotFound
|
// RecordNotFound
|
||||||
|
@ -938,7 +938,7 @@ db.Where("name = ?", "hello world").First(&User{}).Error == gorm.RecordNotFound
|
||||||
db.Where("name = ?", "hello world").First(&user).RecordNotFound()
|
db.Where("name = ?", "hello world").First(&user).RecordNotFound()
|
||||||
|
|
||||||
if db.Model(&user).Related(&credit_card).RecordNotFound() {
|
if db.Model(&user).Related(&credit_card).RecordNotFound() {
|
||||||
// no credit card found error handling
|
// no credit card found error handling
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -973,9 +973,9 @@ If you have an existing database schema, and the primary key field is different
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Animal struct {
|
type Animal struct {
|
||||||
AnimalId int64 `gorm:"primary_key:yes"`
|
AnimalId int64 `gorm:"primary_key:yes"`
|
||||||
Birthday time.Time
|
Birthday time.Time
|
||||||
Age int64
|
Age int64
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue