mirror of https://github.com/go-gorm/gorm.git
Update Document
This commit is contained in:
parent
118c8b836f
commit
9a79822ff2
|
@ -1,40 +1,12 @@
|
||||||
# Summary
|
# Summary
|
||||||
|
|
||||||
* [GORM GITHUB](http://github.com/jinzhu/gorm)
|
* [GORM GITHUB](http://github.com/jinzhu/gorm)
|
||||||
* [Getting Start](README.md)
|
* [Getting Started with GORM](README.md)
|
||||||
{% for path, chapter in book.chapters %}
|
{% for path, chapter in book.chapters %}
|
||||||
* [{{ chapter.name }}]({{ path }})
|
* [{{ chapter.name }}]({{ path }})
|
||||||
{% for section in chapter.sections %}
|
{% for section in chapter.sections %}
|
||||||
* [{{ section.name }}]({{ section.path }})
|
* [{{ section.name }}]({{ section.path }})
|
||||||
|
{% if section["sections"] %}{% for subsection in section.sections %}
|
||||||
|
* [{{ subsection.name }}]({{ subsection.path }})
|
||||||
|
{% endfor %}{% endif %}{% endfor %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
* [Database](database/database.md)
|
|
||||||
* [Connecting to a Database](database/connect-database.md)
|
|
||||||
* [Migration](database/migration.md)
|
|
||||||
* [Schema]()
|
|
||||||
* [Models]()
|
|
||||||
* [Model Defination]()
|
|
||||||
* [Naming Conventions & Overriding]()
|
|
||||||
* [Associations]()
|
|
||||||
* [Belongs To]()
|
|
||||||
* [Has One]()
|
|
||||||
* [Has Many]()
|
|
||||||
* [Many To Many]()
|
|
||||||
* [Polymorphism]()
|
|
||||||
* [Association Mode]()
|
|
||||||
* [CRUD: Reading and Writing Data]()
|
|
||||||
* [Create]()
|
|
||||||
* [Query]()
|
|
||||||
* [Preloading (Eager Loading)]()
|
|
||||||
* [Update]()
|
|
||||||
* [Delete / Soft Delete]()
|
|
||||||
* [Callbacks]()
|
|
||||||
* [Advanced Usage]()
|
|
||||||
* [Error Handling]()
|
|
||||||
* [Transactions]()
|
|
||||||
* [Raw SQL & SQL Builder]()
|
|
||||||
* [Composite Primary Key]()
|
|
||||||
* [Overriding Logger]()
|
|
||||||
* [Development]()
|
|
||||||
* [Write Plugins]()
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
"url": "https://github.com/jinzhu/gorm"
|
"url": "https://github.com/jinzhu/gorm"
|
||||||
},
|
},
|
||||||
"edit-link": {
|
"edit-link": {
|
||||||
"base": "https://github.com/jinzhu/gorm/edit/gh-pages",
|
"base": "https://github.com/jinzhu/gorm/edit/master",
|
||||||
"label": "Edit This Page"
|
"label": "Edit This Page"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -28,9 +28,55 @@
|
||||||
"database/database.md": {
|
"database/database.md": {
|
||||||
"name": "Databae",
|
"name": "Databae",
|
||||||
"sections": [
|
"sections": [
|
||||||
{"name": "Connection to a Database", "path": "database/connect-database.md"},
|
{"name": "Database Connection", "path": "database/connect-database.md"},
|
||||||
{"name": "Migration", "path": "database/migration.md"}
|
{"name": "Migration", "path": "database/migration.md"}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"models/models.md": {
|
||||||
|
"name": "Models",
|
||||||
|
"sections": [
|
||||||
|
{"name": "Model Defination", "path": "models/defination.md"},
|
||||||
|
{"name": "Naming Conventions & Overriding", "path": "models/naming-conventions.md"},
|
||||||
|
{"name": "Associations", "path": "associations/associations.md", "sections":
|
||||||
|
[
|
||||||
|
{"name": "Belongs To", "path": "associations/belongs-to.md"},
|
||||||
|
{"name": "Has One", "path": "associations/has-one.md"},
|
||||||
|
{"name": "Has Many", "path": "associations/has-many.md"},
|
||||||
|
{"name": "Many To Many", "path": "associations/many-to-many.md"},
|
||||||
|
{"name": "Polymorphism", "path": "associations/polymorphism.md"},
|
||||||
|
{"name": "Association Mode", "path": "associations/association-mode.md"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"curd/curd.md": {
|
||||||
|
"name": "CRUD: Reading and Writing Data",
|
||||||
|
"sections": [
|
||||||
|
{"name": "Create", "path": "curd/create.md"},
|
||||||
|
{"name": "Query", "path": "curd/query.md"},
|
||||||
|
{"name": "Preloading (Eager Loading)", "path": "curd/preloading.md"},
|
||||||
|
{"name": "Update", "path": "curd/update.md"},
|
||||||
|
{"name": "Delete / Soft Delete", "path": "curd/delete.md"}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"callbacks/callbacks.md": {
|
||||||
|
"name": "Callbacks"
|
||||||
|
},
|
||||||
|
"advanced/advanced.md": {
|
||||||
|
"name": "Advanced Usage",
|
||||||
|
"sections": [
|
||||||
|
{"name": "Error Handling", "path": "advanced/error-handling.md"},
|
||||||
|
{"name": "Transactions", "path": "advanced/transactions.md"},
|
||||||
|
{"name": "Raw SQL & SQL Builder", "path": "advanced/sql-builder.md"},
|
||||||
|
{"name": "Composite Primary Key", "path": "advanced/compose-primary-key.md"},
|
||||||
|
{"name": "Overriding Logger", "path": "advanced/logger.md"}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"development/development.md": {
|
||||||
|
"name": "Development",
|
||||||
|
"sections": [
|
||||||
|
{"name": "Write Plugins", "path": "development/plugins.md"}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,66 @@
|
||||||
### Connecting To A Database
|
# Database Connection
|
||||||
|
|
||||||
|
<!-- toc -->
|
||||||
|
|
||||||
|
## Connecting to a database
|
||||||
|
|
||||||
|
#### MySQL
|
||||||
|
|
||||||
|
**NOTE** don't forgot params `parseTime` to handle data type `time.Time`, [more support parameters](https://github.com/go-sql-driver/mysql#parameters)
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
)
|
||||||
|
func main() {
|
||||||
|
db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### PostgreSQL
|
||||||
|
|
||||||
```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/mattn/go-sqlite3"
|
|
||||||
)
|
)
|
||||||
|
func main() {
|
||||||
func init() {
|
|
||||||
db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
||||||
// db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
|
|
||||||
// db, err := gorm.Open("sqlite3", "/tmp/gorm.db")
|
|
||||||
|
|
||||||
// Use existing database connection
|
|
||||||
dbSql, err := sql.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
|
||||||
db, err := gorm.Open("postgres", dbSql)
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Sqlite3
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// Get database connection handle [*sql.DB](http://golang.org/pkg/database/sql/#DB)
|
import (
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
func main() {
|
||||||
|
db, err := gorm.Open("sqlite3", "/tmp/gorm.db")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Write Dialect for unsupported databases
|
||||||
|
|
||||||
|
GORM officially support above databases, for unsupported databaes, you could write a dialect for that.
|
||||||
|
|
||||||
|
Refer: https://github.com/jinzhu/gorm/blob/master/dialect.go
|
||||||
|
|
||||||
|
|
||||||
|
## Generic database object *sql.DB
|
||||||
|
|
||||||
|
[*sql.DB](http://golang.org/pkg/database/sql/#DB)
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Get generic database object *sql.DB to use its functions
|
||||||
db.DB()
|
db.DB()
|
||||||
|
|
||||||
// Then you could invoke `*sql.DB`'s functions with it
|
// Connection Pool
|
||||||
db.DB().Ping()
|
|
||||||
db.DB().SetMaxIdleConns(10)
|
db.DB().SetMaxIdleConns(10)
|
||||||
db.DB().SetMaxOpenConns(100)
|
db.DB().SetMaxOpenConns(100)
|
||||||
|
|
||||||
// Disable table name's pluralization
|
// Ping
|
||||||
db.SingularTable(true)
|
db.DB().Ping()
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,22 +1,5 @@
|
||||||
## Database
|
# Database
|
||||||
|
|
||||||
{% for section in book.chapters["database/database.md"].sections %}
|
{% for section in book.chapters["database/database.md"].sections %}
|
||||||
* [**{{section.name}}**](../{{section.path}})
|
* [**{{section.name}}**](../{{section.path}})
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{{book.chapters}}
|
|
||||||
|
|
||||||
<script>
|
|
||||||
window.chapters = {}
|
|
||||||
{% for path, chapter in book.chapters %}
|
|
||||||
chapters["{{path}}"] = {}
|
|
||||||
chapters["{{path}}"]["name"] = "{{chapter.name}}"
|
|
||||||
chapters["{{path}}"]["sections"] = [];
|
|
||||||
{% for section in chapter.sections %}
|
|
||||||
var section = {
|
|
||||||
"name": "{{section.name}}",
|
|
||||||
"path": "{{section.path}}",
|
|
||||||
}
|
|
||||||
chapters["{{path}}"]["sections"].push(section);
|
|
||||||
{% endfor %}
|
|
||||||
{% endfor %}
|
|
||||||
</script>
|
|
||||||
|
|
|
@ -57,3 +57,33 @@ db.Model(&User{}).ModifyColumn("description", "text")
|
||||||
```go
|
```go
|
||||||
db.Model(&User{}).DropColumn("description")
|
db.Model(&User{}).DropColumn("description")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Add Foreign Key
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Add foreign key
|
||||||
|
// 1st param : foreignkey field
|
||||||
|
// 2nd param : destination table(id)
|
||||||
|
// 3rd param : ONDELETE
|
||||||
|
// 4th param : ONUPDATE
|
||||||
|
db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Indexes
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Add index
|
||||||
|
db.Model(&User{}).AddIndex("idx_user_name", "name")
|
||||||
|
|
||||||
|
// Multiple column index
|
||||||
|
db.Model(&User{}).AddIndex("idx_user_name_age", "name", "age")
|
||||||
|
|
||||||
|
// Add unique index
|
||||||
|
db.Model(&User{}).AddUniqueIndex("idx_user_name", "name")
|
||||||
|
|
||||||
|
// Multiple column unique index
|
||||||
|
db.Model(&User{}).AddUniqueIndex("idx_user_name_age", "name", "age")
|
||||||
|
|
||||||
|
// Remove index
|
||||||
|
db.Model(&User{}).RemoveIndex("idx_user_name")
|
||||||
|
```
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Models
|
||||||
|
|
||||||
|
{% for section in book.chapters["models/models.md"].sections %}
|
||||||
|
* [**{{section.name}}**](../{{section.path}})
|
||||||
|
{% if section["sections"] %}{% for subsection in section.sections %}
|
||||||
|
* [**{{ subsection.name }}**]({{ subsection.path }})
|
||||||
|
{% endfor %}{% endif %}
|
||||||
|
{% endfor %}
|
Loading…
Reference in New Issue